Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to resolve SQL SUM not working correctly?

I have an SQL data table called sales that records product, quantity sold, unit price, and the date of the sale. I would like to create a view with monthly sales for chairs from 2000-2010. That is, for every unique month and year pair I would like to sum up quantity * unit_price where the product = "CHAIR". My attempt is the following:

CREATE VIEW MONTHLY_SALES(product, monthly_sales, month, year) AS (
SELECT product, SUM(quantity * unit_price) AS monthly_sales,
           TO_CHAR(sale_date, 'Month') AS month, EXTRACT(YEAR FROM sale_date) AS year
FROM sales
WHERE sale_date BETWEEN '2000-01-01' AND '2010-12-31' AND product = 'CHAIR'
GROUP BY quantity, unit_price, article, sale_date);
    
SELECT *
FROM MONTHLY_SALES;

I would expect to sum over the same month and year and get something like:

product | monthly_sales | month | year
Chair     100             January 2001
Chair     110             February 2001
Chair     120             March    2001
Chair     300             April    2001

But I’m seeing different monthly_sales for the same month-year pairs so I don’t know why the SUM isn’t working. Any assistance to fix this would be much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

===============================================================

Sample Data:

product | quantity | unit_price | sales_date
Chair     1             40        2001-01-01
Chair     3             40        2001-01-02
Chair     4             40        2001-02-01
Chair     1             40        2001-02-05

Expected Output:

product | monthly_sales | month   | year
Chair     160             January   2001
Chair     200             February  2001

>Solution :

SELECT product, SUM(quantity * unit_price) AS monthly_sales,
           EXTRACT(MONTH from sales_date) AS month, EXTRACT(YEAR FROM sales_date) AS year
FROM sales
WHERE sales_date BETWEEN '2000-01-01' AND '2010-12-31' AND product = 'CHAIR'
group by product, year, month

I’ve created a fiddle

Your group by is wrong – you’re not grouping by product, month and year. What you’re asking is "every time you see a change in any of quantity, price, article (whatever that is) and date, perform the sum". That will create lots of duplicates (if you have two sales in a given year/month combination, your group by says "create a new row because you’ve seen a change in sales date).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading