views:

416

answers:

10

I have the following table:

memberid  
2
2
3
4
3

...and I want the following result:

memberid    count
2           2
3           1    ---Edit by gbn: do you mean 2?
4           1

I was attempting to use:

  SELECT MemberID, 
         COUNT(MemberID) 
    FROM YourTable 
GROUP BY MemberID

...but now I want find which record which has maximum count. IE:

memberid   count
2          2
+1  A: 

SELECT MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID

astander
looks completely correct to me
Atmocreations
@Atmocreations: not lines 1 or 2, only the 3rd statement with the aggregate. You have to consider the edit history.
gbn
Sorry, the original formatting of the question was what put me off. That is why i fixed it.
astander
Your first and second version are still wrong in the context of the question. I would delete them.
Maximilian Mayerl
+12  A: 
SELECT memberid, COUNT(*) FROM TheTable GROUP BY memberid

Although, it won't work for your desired output because you have "memberid = 3" twice.

Edit: After late update to question...

SELECT TOP 1 WITH TIES    --WITH TIES will pick up "joint top". 
    memberid, COUNT(*)
FROM
    TheTable 
GROUP BY 
    memberid
ORDER BY
    COUNT(*) DESC
gbn
you didn't answer the second part of the question
Dave
@Dave: you mean the one added an hour or after I answered?
gbn
WITH TIES. Beautiful.
Yoav
Should you use * or 1/null for performance?
astander
@astander: no difference at all, same as EXISTS. See this please: http://stackoverflow.com/questions/1221559/count-vs-count1/1221649#1221649
gbn
A: 

SELECT count(column_name) FROM your_table;

Davit Siradeghyan
This is an incorrect answer
Raj More
A: 

Do it like this:

SELECT memberid, COUNT(memberid) AS [count] FROM [Table] GROUP BY memberid
J. Random Coder
+1  A: 

What if there is a tie (or more) for the max? Do you want to display one or all?

This is how I would do this

SELECT memberid, COUNT(1)
FROM members
GROUP BY memberid
HAVING COUNT(1) = (
      SELECT MAX(result.mem_count)
      FROM ( 
       SELECT memberid, COUNT(1) as mem_count
       FROM members
       GROUP BY memberid
      ) as result
       )

I would love to see a more efficient approach though.

Hassan Voyeau
A: 

You need to use a subselect:

SELECT MemberID, MAX(Count) FROM
    (SELECT MemberID, COUNT(MemberID) Count FROM YourTable GROUP BY MemberID)
GROUP BY MemberID

The second group by is needed to return both, the count and the MemberID.

Oliver Hanappi
No, this is just bad.
erikkallen
Can you explain that a little bit more detailed than just claiming, that it is bad?
Oliver Hanappi
I just found your solution. I think yours is even worse, taking into account that there may be more than one result which has the max count.
Oliver Hanappi
@Oliver: see my answer using WITH TIES
gbn
I like your WITH TIES solution, it's very elegant. I'm just not agreeing with erikkallen claiming my solution "is just bad", what is quite funny because his is not even bad but wrong ^^
Oliver Hanappi
@Oliver: Your solution will select each member with his count. Your second grouping by MemberId serves no purpose whatsoever. I have added WITH TIES to my solution, but it's not clear from the OP whether that was desired. For a correct solution which is not specific to SQL server, see Hassan's solution.
erikkallen
"with his count" should be: "with the max count".
erikkallen
@Oliver: erikkallen is right. Your derived table contains one row for each MemberID, because of the GROUP BY. As a result, your outer aggregation does no aggregation. The MAX value of Count is taken over the exactly one row for each MemberID value.In addition, your derived table doesn't have an alias, which it needs, so the query you posted will fail with a syntax error even before it has a chance to produce the wrong answer.(In any case, I agree that erikkallen should initially have said more than "this is just bad.")
Steve Kass
A: 

This should do the trick with no subselects required:

select top 1 memberid, COUNT(*) as counted 
from members
group by memberid
order by counted desc
Alex Bagnolini
Same problem as erikkallen's soluation: What if there are more results that have the same (maximum) count?
Oliver Hanappi
In the example provided a unique line is in the result, so my query does indeed what the OP asked for. Unless, of course, he meant the memberId = 3 to count just 1, in which case all the answers here are wrong.
Alex Bagnolini
A: 

Can be done quite easy:

SELECT TOP 1 MemberId, COUNT(*) FROM YourTable GROUP BY MemberId ORDER By 2 DESC
erikkallen
What if there are more results that have the same (maximum) count?
Oliver Hanappi
If, and I say IF, you wnat more than one, do SELECT TOP 1 WITH TIES ...
erikkallen
A: 

I believe the original poster requested 2 result sets.

The only way I know of to get this (in SQL Server) is to dump the original records into a temp table and then do a SELECT and MAX on that. I do welcome an answer that requires less code!

-- Select records into a temp table
SELECT
    Table1.MemberId
    ,CNT = COUNT(*)
INTO #Temp
FROM YourTable AS Table1
GROUP BY Table1.MemberId
ORDER BY Table1.MemberId

-- Get original records
SELECT * FROM #Temp

-- Get max. count record(s) 
SELECT 
    Table1.MemberId
    ,Table1.CNT
FROM #Temp AS Table1
INNER JOIN (
    SELECT CNT = MAX(CNT)
    FROM #Temp
) AS Table2 ON Table2.CNT = Table1.CNT

-- Cleanup 
DROP TABLE #Temp
Yoav
Yes, they the 2nd requirement was added after 3 or 4 answers...
gbn
Best to use gbn's example using WITH TIES
Yoav
A: 

How about:

SELECT TOP 1 MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID ORDER by count(MemberID) desc

gera