My task is to keep only 100 largest clients in the output, so I need to count number of IDs per customer, and then keep only top 100. The output should contain all the necessary variables, no groupings.
Sample output would look like:
Var1, Var2, ID
aaaaaaa, bbbbbbb, 12345
ccccccc, ddddddd, 12345
...
eeeeeee, fffffff, 67890
Please find my best attempt so far (I am struggling with the limit part for this query):
SELECT
A.Var1,
P.Var2,
P.ID
FROM Database1 P
INNER JOIN Database2 A
ON P.ID = A.ID
INNER JOIN
(
SELECT ID, COUNT(*) AS IDs_per_BAN
FROM Database1
GROUP BY ID
) NUM_COUNT
ON P.ID = NUM_COUNT.ID
WHERE
(
???
)
ORDER BY NUM_COUNT.IDs_per_BAN DESC