I have 3 Tables named Transactions,Customers and Tickets. Each ticket has a category (Theater,Cinema or Concert). I want to find the name of the person that made the most purchases foreach category:
For example:
Category | Name | Purchases Theater George 5 Cinema Tom 12 Cinema Mary 12 Concert John 7
WITH PurchasesSum AS (
SELECT Category,Customers.Name,COUNT(Transactions.Customer_ID) AS Purchases,
RANK() OVER (ORDER BY COUNT(Transactions.Customer_ID) DESC) AS Rank
FROM Transactions
INNER JOIN Customers
ON Transactions.Customer_ID= Customers.ID
INNER JOIN Tickets
ON Transactions.Ticket_num = Tickets.Ticket_num
GROUP BY Name,Category
)
SELECT Name, Purchases,Category
FROM PurchasesSum
ORDER BY Category ASC;
So far I got to this point, but it returns all of them
For example:
Category | Name | Purchases Cinema Tom 12 Cinema Mary 12 Cinema John 3 Concert John 7 Concert Tom 1 Theater George 5 Theater Mary 2