-4

I am trying to see the number of products sold to different companies by month and year.

For example:

I have a dataset where I have a long list of products. I would like to see two different columns where the first column shows a count of all those products and a second column that shows the company that was sold to by month and year

That is my query:

SELECT MONTHNAME(date) AS Month, COUNT(*), company
FROM database.products
WHERE     YEAR(date) = '2022'
GROUP BY  MONTH(date)
WITH ROLLUP); 

Expected Result:

Year Month Products Company
2022 January 654 walmart
2022 January 895 Winco
2022 February 562 Ross
2022 February 456 Best Buy

1 Answers1

1

Just add the rest of the fields...?

SELECT YEAR(date) AS Year, MONTHNAME(date) AS Month, COUNT(*), company
FROM database.products
WHERE YEAR(date) = '2022'
GROUP BY YEAR(date), MONTH(date), company
WITH ROLLUP
Joan Lara
  • 1,362
  • 8
  • 15