Skip to content Skip to sidebar Skip to footer

Count, Order Desc And Select Top 5

SQL Server 2012 We have a table, like such: ticket, type ------------------ 1234, hardware 1543, software 8859, network 5832, hardware 4900, hardware 8403, software 7859, network 4

Solution 1:

In SQL Server you can use TOP to select a certain number of rows along with an order by to get the proper records:

select top 5 type, count(*) Total
from yourtable
groupby type
orderby total desc

See SQL Fiddle with Demo

Solution 2:

select*from (
    select type, count(*) 
    fromtablegroupby type
    orderby2desc
)
where rownum <=5

Post a Comment for "Count, Order Desc And Select Top 5"