user2637506
user2637506

Reputation: 225

how do we order by and select top 20% records in an order in sql

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

Answers (1)

David Starkey
David Starkey

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

Related Questions