Reputation: 225
SELECT TOP (20)percent count(*) FROM (SELECT [col1],col2
FROM [report].[detailsprovider]
WHERE [col3] = 2 group by [col1] as t order by t.col2
I am trying to do this, but getting stuck in order by and group by.
I want the distinct count of col1 with top 20% ordered(based on col2) count/list of rows
Upvotes: 1
Views: 272
Reputation: 1840
SELECT COUNT(*), col1
FROM (SELECT TOP 20 PERCENT col1 FROM myTable WHERE col3=2
ORDER BY col2) a
GROUP BY col1
NOTE: This is for TSQL. I'm not sure what other SQL flavors contain PERCENT
Upvotes: 0