SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Getting Top 5 based on the count
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mahesh.sanka
Starting Member

India
18 Posts

Posted - 08/19/2013 :  08:01:15  Show Profile  Reply with Quote
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 - 08/19/2013 :  08:11:38  Show Profile  Reply with Quote
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

India
18 Posts

Posted - 08/19/2013 :  08:16:37  Show Profile  Reply with Quote
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 - 08/19/2013 :  08:31:32  Show Profile  Reply with Quote
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

India
22761 Posts

Posted - 08/19/2013 :  08:53:27  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000