CanMan2004 Posted December 11, 2006 Share Posted December 11, 2006 Hi allI have a sql table which looks likeID NAME USERID1 Dave 43245662 John 12456903 Sarah 78966454 Dave 43245665 Henry 43245666 Sarah 12456907 Bill 43245668 Dave 8754446What I want to do is to perform a query which will return a list of names with the most popular at the top with the least most popular at the bottom.The twist is that if a user has added the same name more than once, then it should count it as one, for example, if it was counting the name Dave and the following was storedID NAME USERID1 Dave 12456902 Dave 43245663 Dave 1245690Then it would count only 2 rows, as the NAME Dave was entered by USERID 1245690 twice.With the followingID NAME USERID1 Dave 12456902 Dave 43245663 Dave 12456904 Sarah 3245565It would returnDave - 2Sarah - 1Does this make sense? I have been racking my brain all day to perform this sort of query, but am totally stuck.Any help would be aceThanksEd Quote Link to comment Share on other sites More sharing options...
freeloader Posted December 11, 2006 Share Posted December 11, 2006 What do you mean by 'the most popular'? Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 12, 2006 Share Posted December 12, 2006 SELECT temp.name, COUNT(temp.name) as countFROM (SELECT DISTINCT name, userid FROM `tableName`) as tempGROUP BY name Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 12, 2006 Author Share Posted December 12, 2006 Thanks, that's great.Is it possible to count the total rows in the database but if a row has the same "NAME" and "USERID" then it would count that as one row Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 12, 2006 Author Share Posted December 12, 2006 For example, if the database looked likeID NAME USERID1 Dave 1122 Sarah 2323 Bill 8764 Dave 2355 Sarah 232It would count 4 rows as a total, this is because row 2 and row 5 have the same NAME and USERID.Any help would be great Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 12, 2006 Share Posted December 12, 2006 Basically the same query as before w/o the GROUP BY clause (not tested):SELECT COUNT(temp.*) as countFROM (SELECT DISTINCT name, userid FROM `tableName`) as tempBy the way, is there any reason you can't just delete the duplicates from the table and add some logic to prevent duplicates in the future. That seems a better approach to me. Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 Hi allI get an error when I use[code]SELECT COUNT(temp.*) as count FROM (SELECT DISTINCT name, userid FROM users) as temp[/code]Does anyone know why? Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 14, 2006 Author Share Posted December 14, 2006 Can anyone help? Quote Link to comment Share on other sites More sharing options...
mjlogan Posted December 14, 2006 Share Posted December 14, 2006 SELECT COUNT(temp.name) as count FROM (SELECT DISTINCT name, userid FROM users) as temp ??? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.