Question

Select Distinct id as personID, count(tid) as Sales
From salesTable
Group by personID;

Current Results are: (I need the top sales to only show.)

 personID  | Sales
_____________________
   1000      | 2
   1020      | 1
   1040      | 2
   1060      | 1
   1080      | 2
   1140      | 1
   1160      | 1
   _________________

The salesTable looks like this:

id  | tid
_________
1000|201
1020|202
1040|203
1000|204
1060|205
1080|206
1040|207
1080|208
1140|209
1160|210

My desired results should look like this:

personID  | Sales
_____________________
   1000      | 2
   1040      | 2
   1080      | 2
  _________________
Was it helpful?

Solution

select  id as personID
,       count(tid) as Sales
From    salesTable
group by 
        id
having  count(tid) =
        (
        select  max(Sales)
        from    (
                select  count(tid) as Sales
                from    salesTable
                group by
                        id
                ) SubQueryAlias
        )

OTHER TIPS

Slightly simpler and ordered:

SELECT id as personID
      ,count(tid) AS Sales
FROM   salesTable
GROUP  BY id
HAVING count(tid) =
   (
   SELECT count(tid)
   FROM   salesTable
   GROUP  BY id
   ORDER  BY count(tid) DESC
   LIMIT  1
   )
ORDER  BY personID;

->SQLfiddle demo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top