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.
Author |
Topic |
bijan
Starting Member
3 Posts |
Posted - 2015-04-09 : 09:13:06
|
Hi,I need help.Does anyone know how to return say, top 5 of a grouped data returned by sql sattement?For example, I have written a SQL statement which returns number of transactions for say various skills within a department but I would like to return the top 5 of those skills in the department.Many thanks,Bijan |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2015-04-09 : 15:30:59
|
[code]SELECT TOP (5) SkillsFROM DepartmentTableGROUP BY SkillsORDER BY COUNT(Transactions) DESC[/code]If that is not what you are looking for post some sample data. |
|
|
bijan
Starting Member
3 Posts |
Posted - 2015-04-10 : 04:24:45
|
Thanks very much for your reply, but this is not what I want. Please have a look at below. This is a list of Codes and Trans NoBCT1 413BCT1 277BCT1 236BCT1 82BCT1 69BCT1 68BCT1 67BCT1 67BCT1 63BCT1 55BCT1 51BCT1 50BCT1 50BCT1 45BCT1 42BCT1 41BCT1 38BCT1 35BCT1 34BCT1 31BCT1 30BCT1 27BCT1 26BCT1 24BCT1 24BCT1 23BCT1 22BCT1 22BCT1 22BCT1 22BCT1 21BCT1 19BCT1 19BCT1 18BCT1 18BCT1 17BCT1 17BCT1 17BCT1 17BCT1 17BCT1 17BCT1 17BCT1 16BCT1 16BCT1 16BCT1 16BCT1 16BCT1 15BCT1 15BCT1 15BCT1 15BCT1 15BCT1 14BCT1 14BCT1 14BCT1 14BCT1 14BCT1 14BCT1 14BCT1 14BCT1 13BCT1 13BCT1 13BCT1 12BCT1 12BCT1 12BCT1 12BCT1 12BCT1 12CSS1 18CSS1 7CSS1 7CSS1 5CSS1 5CSS1 5CSS1 4CSS1 3CSS1 3CSS1 3CSS1 3CSS1 3CSS1 3CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2CSS1 2MED1 23MED1 21MED1 21MED1 20MED1 18MED1 18MED1 18MED1 17MED1 17MED1 15MED1 14MED1 14MED1 14MED1 14MED1 13MED1 13MED1 13MED1 12MED1 12MED1 12MED1 12MED1 12MED1 11MED1 11MED1 11MED1 10MED1 10MED1 10MED1 10MED1 10MED1 10MED1 10MED1 10MED1 9MED1 9MED1 8MED1 7MED1 7MED1 7MED1 7I want to return the following:BCT1 413BCT1 277BCT1 236BCT1 82BCT1 69BCT1 68BCT1 67BCT1 67BCT1 63BCT1 55CSS1 18CSS1 7CSS1 7CSS1 5CSS1 5CSS1 5CSS1 4CSS1 3CSS1 3CSS1 3MED1 23MED1 21MED1 21MED1 20MED1 18MED1 18MED1 18MED1 17MED1 17MED1 15which is top 10 for each of the codes.I hope this makes sense!many thanks, |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2015-04-10 : 08:02:55
|
select col1,col2 from(select col1,col2, row_number() over (partition by col1 order by col2 desc) as sno from table) as twhere sno<=10MadhivananFailing to plan is Planning to fail |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2015-04-10 : 08:19:43
|
Madhivanan's suggestion would be what I would do as well, but the ROW_NUMBER function is available only in SQL 2005 or later. Since you posted to SQL 2000 forum, you are perhaps on SQL 2000. In that case, you will need to do something like this (untested - i don't have a sql 2000 installation)SELECT code, TransNoFROM table aWHERE TransNo IN ( SELECT TOP 10 TransNo FROM table b WHERE a.code = b.code ORDER BY TransNo DESC ) |
|
|
bijan
Starting Member
3 Posts |
Posted - 2015-04-10 : 10:47:06
|
Thank you very much both for the suggestions.I have not tried them yet, but I will do next week and will definitely let you know.Thank you again for taking the time to come up with solutions.Regards,Bijan |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2015-04-13 : 06:22:09
|
quote: Originally posted by James K Madhivanan's suggestion would be what I would do as well, but the ROW_NUMBER function is available only in SQL 2005 or later. Since you posted to SQL 2000 forum, you are perhaps on SQL 2000. In that case, you will need to do something like this (untested - i don't have a sql 2000 installation)SELECT code, TransNoFROM table aWHERE TransNo IN ( SELECT TOP 10 TransNo FROM table b WHERE a.code = b.code ORDER BY TransNo DESC )
Thanks I did not notice it was posted in 2000 forum. Here are some other methods http://beyondrelational.com/modules/2/blogs/70/posts/10845/return-top-n-rows.aspxMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|