Query to see only first several records

C

Charles W

I have a customer order table that contains customer names and their orders

Customer Order

A 10
A 9
A 3
B 5
B 10
B 9

Is it possible to set up a query to see only the first 2 top orders of each
customers (or any number of top orders)?

Thanks in advance.

Charles
 
K

Ken Snell \(MVP\)

Define "top".

You use a subquery to get the subset of Order values for each customer,
based on what you mean by "top" orders.

For example, assuming that you want "top" to mean the largest numbers for
the Order field:

SELECT CustomerOrder.Customer, CustomerOrder.Order
FROM CustomerOrder
WHERE CustomerOrder.Order IN
(SELECT TOP 2 T.Order
FROM CustomerOrder T
WHERE T.Customer = CustomerOrder.Customer
ORDER BY T.Order DESC);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top