Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Getting Top 5 based on the count

Author  Topic 

mahesh.sanka
Starting Member

18 Posts

Posted - 2013-08-19 : 08:01:15
Hi,

I am using the following query

select top(5) COUNT(incident),name from company_info group by name
order by COUNT(incident) desc ;

to get most repeated incidents.

count incident
17 ghi
15 def
14 abc
11 z
9 x

I want to get the same output with out using order by query.


Thanks & Regards,
Mahesh Kumar Sanka

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-08-19 : 08:11:38
That doesn't make sense. TOP always requires an ORDER BY. How else will it know what your definition of "TOP" is?
Go to Top of Page

mahesh.sanka
Starting Member

18 Posts

Posted - 2013-08-19 : 08:16:37
Hi ,

Thanks for your reply. Then how to get top 5 rows with out using top query.

quote:
Originally posted by LoztInSpace

That doesn't make sense. TOP always requires an ORDER BY. How else will it know what your definition of "TOP" is?



Thanks & Regards,
Mahesh Kumar Sanka
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2013-08-19 : 08:31:32
You can use ORDER BY then your client can read only the first 5 rows but there's no real reason to do that.
What exactly is the issue? Your problem appears to be "I need the top 5 of something but I cannot define what top means".
The answer to the question appears to be exactly the query you posted - so why not use that?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-08-19 : 08:53:27
quote:
Originally posted by mahesh.sanka

Hi ,

Thanks for your reply. Then how to get top 5 rows with out using top query.

quote:
Originally posted by LoztInSpace

That doesn't make sense. TOP always requires an ORDER BY. How else will it know what your definition of "TOP" is?



Thanks & Regards,
Mahesh Kumar Sanka


Without order by clause the result would be random

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -