TOP seven data and grouping

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 7
Reputation: Cano82 is an unknown quantity at this point 
Solved Threads: 0
Cano82 Cano82 is offline Offline
Newbie Poster

TOP seven data and grouping

 
0
  #1
Dec 31st, 2007
The tables are joined. I want to display last 7 data with grouping their category

My tables:

Category: categoryId, categoryName
Order: orderId, catgoryId, product
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 35
Reputation: mellamokb is an unknown quantity at this point 
Solved Threads: 5
mellamokb mellamokb is offline Offline
Light Poster

Re: TOP seven data and grouping

 
0
  #2
Jan 3rd, 2008
The tables are joined. I want to display last 7 data with grouping their category
I am going to guess this means you want the last 7 orders by category, in which case there would need to be some sort of date field, say Order.orderDate, to identify what "last" means:

(untested)
  1. SELECT
  2. o1.[orderId],
  3. o1.[categoryId],
  4. c.[categoryName],
  5. o1.[product]
  6. FROM
  7. [ORDER] o1
  8. INNER JOIN
  9. [Category] c ON [Category].[categoryId] = o1.[categoryId]
  10. WHERE
  11. o1.[orderId] IN (SELECT TOP 7 o2.[orderId] FROM [ORDER] o2 WHERE o2.[categoryId] = o1.[categoryId] ORDER BY o2.[OrderDate] DESC)

~ mellamokb
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC