Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to fetch data from db in groups using group by.
Group can have any number of records.
I want only top 20 of such groups.

Something like:-

select top 20 from table
where conditions
group by someId, someOtherId.
Posted
Comments
CodeBlack 4-Oct-13 9:38am    
Let me clear my self.
For example you have two columns in table A : Group and Member.
Now
Group | Member
A | a
A | b
A | c
B | d
B | e
B | f
C | g
C | h

Now when you say top 2 groups than it should return all the data which are belongs to group 'A' and group 'B' right ?
PIEBALDconsult 4-Oct-13 9:49am    
If there are 50 cats, 40 dogs, and 60 elephants, you want to return the top 20 cats, the top 20 dogs, and the top 20 elephants?
Look into ROW_NUMBER().

1 solution

You almost done.
SQL
SELECT TOP 20 SomeColumn,COUNT(SomeOtherColumn) AS SomeOtherColumn_Count FROM Table_Name
GROUP BY SomeColumn;

FYI TOP[^] & GROUP BY[^] @ www.w3schools.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900