3

I want to find top 2 customers with maximum orders.
The table looks like:

CustomerId  OrderId ProductId
101         1       A
101         3       B
101         4       C
102         9       D
102         9       E
103         11      E
103         22      F

This is the output that I need from SELECT query:

CustomerId  OrderId 
101         1
101         3
101         4
103         11
103         22  

The solution is just not clicking to my mind...I have kind of reached half-way using following query -

SELECT CustomerId, OrderId
FROM dbo.CustomerOrder
GROUP BY CustomerId, OrderId

which just gives me distinct pairs of CustomerId, OrderId.

Can anyone please help.

3
  • In your desired result 101 appears 3 times but you say that you want top 2 customers only. Pls fix.
    – NoChance
    Oct 29, 2013 at 1:38
  • Because I want to show all orders placed by top 2 customers
    – inutan
    Oct 29, 2013 at 1:41
  • And what happens if you have 2 records at the second place? Which one you choose? – Oct 29, 2013 at 2:03

7 Answers 7

3

Here is the SQL Fiddle example that shows the below code working:

SELECT DISTINCT CO.CustomerId, CO.OrderID FROM 
(
  SELECT TOP(2) COS.CustomerId, COUNT(DISTINCT COS.orderId) as NoOfOrders
  FROM custorders AS COS
  GROUP BY COS.CustomerId
  ORDER BY COUNT(DISTINCT COS.orderId) DESC, CustomerId  DESC
) AS COM 
INNER JOIN custorders AS CO
  ON COM.CustomerId = CO.CustomerId
4
  • I do not think this answer is correct, although it does produce the correct result. You can break this answer by switching the orders for customers 102 and 103. The problem is that customer 102 only has 1 order (both records are orderId 9), but this subselect will treat customer 102 as having 2 orders.
    – rsbarro
    Oct 29, 2013 at 1:51
  • @rsbarro, you are right, this is not fully correct, I had to add DISTINCT while COUNTing as: ORDER BY COUNT(DISTINCT COS.orderId)
    – inutan
    Oct 29, 2013 at 2:00
  • I updated my answer to reflect the concerns, you will now get the correct results if an order id shows more than once.
    – Linger
    Oct 29, 2013 at 2:04
  • @iniki I think COUNT(DISTINCT) is the way to go. I updated my answer to use that approach which limits the query to only 1 subselect.
    – rsbarro
    Oct 29, 2013 at 2:05
1

Try this:

SELECT c.CustomerId, c.OrderId
FROM CustomerOrder c
INNER JOIN
(SELECT TOP 2 WITH TIES CustomerId, COUNT(distinct OrderId) as Count
FROM CustomerOrder
GROUP BY CustomerId
ORDER BY Count DESC) b ON c.CustomerId = b.CustomerId

You could use WITH TIES, For example, if you have 3 customers with the same maximum amount of orders, WITH TIES will retrieve the three, without this you will let one outside, an that may be wrong.

Check this SQL FIDDLE DEMO

1

I would use a subselect to find the customers with the most orders. Here's a working example:

DECLARE @orders AS TABLE(CustomerId INT, OrderId INT, ProductId VARCHAR(10))
INSERT INTO @orders VALUES(101, 1, 'A')
INSERT INTO @orders VALUES(101, 3, 'B')
INSERT INTO @orders VALUES(101, 4, 'C')
INSERT INTO @orders VALUES(102, 9, 'D')
INSERT INTO @orders VALUES(102, 9, 'E')
INSERT INTO @orders VALUES(103, 11, 'E')
INSERT INTO @orders VALUES(103, 22, 'F')

SELECT DISTINCT
    O.CustomerId,
    O.OrderId
FROM @orders O
JOIN (
    SELECT TOP 2 CustomerId, COUNT(DISTINCT(OrderId)) as OrderCount
    FROM @orders
    GROUP BY CustomerId
    ORDER BY COUNT(DISTINCT(OrderId)) DESC, CustomerId
) O2 ON O2.CustomerId = O.CustomerId
ORDER BY O.CustomerId, O.OrderId

In the subselect I added a secondary sort to break ties on order count.

3
  • It doesn't appear this would work correctly. It shows multiple OrderIDs in the result of the query. Here is the SQL Fiddle showing the problem.
    – Linger
    Oct 29, 2013 at 2:15
  • +1 Your solution works as well. ;) We were both missing similar things.
    – Linger
    Oct 29, 2013 at 2:22
  • Yea, for whatever reason this problem was a little harder than it seemed. :) I +1'd your answer as well.
    – rsbarro
    Oct 29, 2013 at 2:25
0
with CustomerOrders as
(
select CustomerId, count(orderId) as NoOfOrders
from dbo.CustomerOrder
group by CustomerId
)
select top 2 * from CustomerOrders
order by NoOfOrders desc
0
SELECT customer.cust_name, order.order_amount
FROM customer inner join order
ON customer.cust_id = order.cust_id
ORDER BY order.order_amount DESC
0
select customername from customers where customerid  in 
   (select top 2 customerid from 
       (select count(*), customerid FROM orders
        group by customerid
        order by count(*) DESC))
0
select customerid,orderid from customerOrders 
group by CustomerId,OrderId 
having count(orderid) =1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.