| Author |
Topic |
|
waqas
Starting Member
5 Posts |
Posted - 2008-04-03 : 09:31:45
|
| How Can I use Top Clause with GROUP BY clause?Here is my simple problem. I have two tables CategoriesProductsI want to know Top 5 Products in CategoryID 1,2,3,4,5Resultset should contain 25 Rows ( 5 top products from each category )I hope someone will help me soon. Its urngent thanks in advance regardsWaqas |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-03 : 10:12:55
|
| [code]SELECT t.*FROM(SELECT ROW_NUMBER() OVER (PARTITION BY CategoryID ORDER BY ProductID DESC) AS RowNo,*FROM Categories cINNER JOIN Products pON p.ProductID=c.ProductID)tWHERE t.RowNo <=5[/code] |
 |
|
|
waqas
Starting Member
5 Posts |
Posted - 2008-04-05 : 21:05:24
|
| Hi visakh I tried to use your query but it doesn't work. Are you missing any thing in the query ?Thanks for your earlier reply with queyr but can you please check it and resent if something is missing in query. have a nice day regardsWaqas |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-06 : 02:41:39
|
quote: Originally posted by waqas Hi visakh I tried to use your query but it doesn't work. Are you missing any thing in the query ?Thanks for your earlier reply with queyr but can you please check it and resent if something is missing in query. have a nice day regardsWaqas
Can you post what error message you got? Also make sure your server is SQL 2005 and you have used a compatability level of 90 using sp_dbcmptlevel command. ROW_NUMBER works only in 2005 with compatibilty level 90. |
 |
|
|
waqas
Starting Member
5 Posts |
Posted - 2008-04-06 : 12:20:46
|
| I am using SQL Server 2005 and Compatibility Level is also 90. I think error is in the query because if you see the error below it is something related to column name or alias. See the error below I am getting in my database. Msg 207, Level 16, State 1, Line 7Invalid column name 'ProductID'.Msg 209, Level 16, State 1, Line 3Ambiguous column name 'CategoryID'.Msg 8156, Level 16, State 1, Line 3The column 'CategoryID' was specified multiple times for 't'.I am using Northwind database so Table Products has column ProductID and CategoryID is also valid column name in Categories Table. Please check your query again, I am also trying to figure out what is error in that time. thanks Waqas |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-06 : 12:41:51
|
| Can you provide the structure of your two tables? That should make it more clear. |
 |
|
|
waqas
Starting Member
5 Posts |
Posted - 2008-04-06 : 14:34:37
|
| Thanks for quick Reply As I just need example query so I am using Northwind database tables for testing query. Later I will change it according to my requirements of actual project. Here are two tables Categories (Table) CategoryID INT (PK) CategoryName NVARCHAR(15)Products (Table) ProductID INT ProductName NVARCHAR(40) UnitPrice MONEY CategoryID INT (FK)I just want top 5 products of each category as one result set. For example if I have 4 categories so result set should display 20 products ( 5 top products of 4 categories ). If its easy to do in T-SQL Procedure than it will also be ok with me as i am using stored procedures already. Thanks for all your replies once again |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-06 : 14:47:49
|
Change like this. I've assumed you want top 5 products based on decreasing order of price else change UnitPrice filed to your desired field.SELECT t.CategoryName,t.ProductNameFROM(SELECT ROW_NUMBER() OVER (PARTITION BY c.CategoryID ORDER BY UnitPrice DESC) AS RowNo,c.CategoryID,c.CategoryName, p.ProductID,p.ProductNameFROM Categories cINNER JOIN Products pON p.CategoryID =c.CategoryID )tWHERE t.RowNo <=5 |
 |
|
|
waqas
Starting Member
5 Posts |
Posted - 2008-04-06 : 15:03:25
|
| Hi Visakh I dont have words to say you thanks for your quick replies and finally solving my problem. I tested your query on Northwind database and now its working. I also modified it for my project according to different tables and fiels and it works there as well. I have to admit you really help me alot to solve my problem. Thanks for all your help. Have a nice day RegardsWaqas |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-07 : 01:47:47
|
quote: Originally posted by waqas Hi Visakh I dont have words to say you thanks for your quick replies and finally solving my problem. I tested your query on Northwind database and now its working. I also modified it for my project according to different tables and fiels and it works there as well. I have to admit you really help me alot to solve my problem. Thanks for all your help. Have a nice day RegardsWaqas
You are welcome . I would suggest you reading this wonderful article by Madhi on the usage of ROW_NUMBER() function in SQL 2005:-[url]http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx[/url] |
 |
|
|
|