The tables are joined. I want to display last 7 data with grouping their category

My tables:

Category: categoryId, categoryName
Order: orderId, catgoryId, product

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)

SELECT
  o1.[orderId],
  o1.[categoryId],
  c.[categoryName],
  o1.[product]
FROM
  [Order] o1
INNER JOIN
  [Category] c ON [Category].[categoryId] = o1.[categoryId]
WHERE
  o1.[orderId] IN (SELECT TOP 7 o2.[orderId] FROM [Order] o2 WHERE o2.[categoryId] = o1.[categoryId] ORDER BY o2.[OrderDate] DESC)

~ mellamokb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.