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
)
SQL display only the highest values of a count function
-
24-03-2022 - |
سؤال
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
_________________
المحلول
نصائح أخرى
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;
لا تنتمي إلى StackOverflow