Select the first N *groups* using mysql
To make it simple, let say I have a table with 2 fields - PERSON_NAME and
COMPANY_ID with those pairs: ('a',1) ('b',1) ('c',2) ('d',2) ('e',3)
PERSON_NAME a and b are working in COMPANY_ID 1 and so on.
I would like to select all rows for the first 2 (or N) COMPANIES - ('a',1)
('b',1) ('c',2) ('d',2)
I don't know how many rows there are for each COMPANY_ID.
Group by won't work here as I need all rows within each COMPANY_ID.
This one works -
SELECT * FROM T
WHERE COMPANY_ID in (SELECT DISTINCT COMPANY_ID
FROM T
ORDER BY 1 DESC
LIMIT N)
But since performance is an issue, it won't suffice (there are about 25k
companies and about 5-15 persons in each company). N is usually 500.
No comments:
Post a Comment