Break it out a little more:
ORDER BY CASE WHEN @orderby = 1 THEN CONVERT(NVARCHAR(30) , ccd.CertEndDate) END ASC,
CASE WHEN @orderby = 2 THEN CONVERT(NVARCHAR(30) , ccd.CertEndDate) END DESC,
tp.lastname ASC,
tp.firstname ASC
You only need the sort order to change on the first field, so don't enclose the others in the CASE
.
It should be noted that we don't include an ELSE
for each CASE
, which means any other value will return NULL
and be discarded from the ORDER BY
.
Try this:
SELECT id,
title,
pollid,
COUNT(pollid) AS clean_count,
group_concat(DISTINCT u.name) AS user
FROM
(
SELECT DISTINCT user_id, pollid
FROM tbl_votes
) AS tmp_tbl
JOIN tbl_polls
ON tbl_polls.id = tmp_tbl.pollid
JOIN tbl_users AS u
ON tmp_tbl.user_id = u.user_id
WHERE not isnull(tmp_tbl.user_id)
GROUP BY pollid
ORDER BY clean_count DESC
This is the sample data that I used:
CREATE TABLE tbl_polls (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50)
);
CREATE TABLE tbl_users (
user_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE tbl_votes (
id INT AUTO_INCREMENT PRIMARY KEY,
pollid INT,
user_id INT,
INDEX (pollid,user_id),
FOREIGN KEY (pollid) REFERENCES tbl_polls(id),
FOREIGN KEY (user_id) REFERENCES tbl_users(user_id)
);
INSERT INTO tbl_polls (title)
VALUES ('Title 1'),('Title 2'),('Title 3');
INSERT INTO tbl_users (name)
VALUES ('Bob'),('Jack'),('Joe'),('Tom');
INSERT INTO tbl_votes (pollid,user_id)
VALUES (1,2),(1,2),(1,2),(1,3),(2,4),(2,1),(2,4),(2,4),(3,1),(2,1),(1,4);
SQL FIDDLE DEMO
I have used group concat to get the names for your list. Furthermore, to speed up your database have you tried adding index to the foreign keys?
B.T.W. I don't understand your comment that some some votes lack pollid and user_id. If that is the case have a look at this.
Best Answer
Unless I'm grossly misunderstanding your structure, a simple join should work fine:
I'm not sure what you mean by
since your where statement only uses votes/stories that have status=1.