0

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
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Mansi
  • 151
  • 4
  • 12

10 Answers10

12
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
  • 422,506
  • 82
  • 585
  • 676
  • you didn't answer the second part of the question – David Oct 10 '09 at 17:28
  • @Dave: you mean the one added an hour or after I answered? – gbn Oct 10 '09 at 17:56
  • @astander: no difference at all, same as EXISTS. See this please: http://stackoverflow.com/questions/1221559/count-vs-count1/1221649#1221649 – gbn Oct 11 '09 at 06:38
1
SELECT MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

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
  • 3,383
  • 4
  • 22
  • 24
0

Do it like this:

SELECT memberid, COUNT(memberid) AS [count] FROM [Table] GROUP BY memberid
J. Random Coder
  • 1,322
  • 11
  • 21
0

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
  • 21,990
  • 3
  • 41
  • 41
  • 1
    Same problem as erikkallen's soluation: What if there are more results that have the same (maximum) count? – Oliver Hanappi Oct 10 '09 at 18:04
  • 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 Oct 10 '09 at 19:34
0

Can be done quite easy:

SELECT TOP 1 MemberId, COUNT(*) FROM YourTable GROUP BY MemberId ORDER By 2 DESC
erikkallen
  • 33,800
  • 13
  • 85
  • 120
0

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
  • 150
  • 1
  • 2
  • 15
0

How about this query:

SELECT TOP 1 MemberID, 
       COUNT(MemberID) 
FROM YourTable 
GROUP BY MemberID
ORDER by count(MemberID) desc
C B
  • 1,677
  • 6
  • 18
  • 20
-1

SELECT count(column_name) FROM your_table;

Davit Siradeghyan
  • 6,053
  • 6
  • 24
  • 29
-1

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
  • 12,046
  • 7
  • 51
  • 68
  • Can you explain that a little bit more detailed than just claiming, that it is bad? – Oliver Hanappi Oct 10 '09 at 17:34
  • 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 Oct 10 '09 at 17:37
  • @Oliver: see my answer using WITH TIES – gbn Oct 10 '09 at 18:08
  • 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 Oct 10 '09 at 18:38
  • 1
    @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 Oct 11 '09 at 01:08
  • "with his count" should be: "with the max count". – erikkallen Oct 11 '09 at 01:10
  • 1
    @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 Oct 11 '09 at 01:52