Created
March 30, 2022 04:13
-
-
Save kunxin-chor/9f5c424f13ecaf0c5ae72a45cc62aa7c to your computer and use it in GitHub Desktop.
Find best selling product for each month
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( SELECT productCode, year(orderDate) as orderYear, month(orderDate) as orderMonth | |
FROM orderdetails | |
JOIN orders on orders.orderNumber = orderdetails.orderNumber | |
GROUP BY productCode, YEAR(orderDate), MONTH(orderDate) | |
HAVING productCode = ( SELECT productCode | |
FROM orderdetails JOIN orders on orderdetails.orderNumber = orders.orderNumber | |
WHERE year(orderDate) = orderYear AND month(orderDate) = orderMonth | |
GROUP BY productCode | |
ORDER BY COUNT(*) DESC | |
LIMIT 1 | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment