-1

I have 3 tables

Sales journal SALES: DATE - Sale date T_CODE - Product code QUAN - Quantity sold

Sales journal

Product journal PRODUCTS: CODE - Product code NAME - Product name Products journal

Prices journal PRICES: T_CODE - Product code DATE - Date of change of price(i.e. the changed price is valid from that date onwards till the next change of price) COST - The price of the product Prices journal

I need to summarize total quantity sold and total value sold for first three months of 2018 Summary

I've tried to construct SQL query for this as follows:

SELECT PRODUCTS.NAME, PRODUCT.T_CODE

(SELECT SUM(SALES.QUAN) WHERE SALES.DATE BETWEEN '01.01.2018' AND '31.01.2018') AS JANUARY_QUANTITY

(SELECT SUM(SALES.QUAN)*PRICES.COST FROM SALES INNER JOIN PRICES ON PRICES.T_CODE = SALES.T_CODE) AS JANUARY_VALUE

(SELECT SUM(SALES.QUAN) WHERE SALES.DATE BETWEEN '01.02.2018' AND '28.02.2018') AS FEBRUARY_QUANTITY

(SELECT SUM(SALES.QUAN)*PRICES.COST FROM SALES INNER JOIN PRICES ON PRICES.T_CODE = SALES.T_CODE) AS FEBRUARY_VALUE

(SELECT SUM(SALES.QUAN) WHERE SALES.DATE BETWEEN '01.03.2018' AND '31.03.2018') AS MARCH_QUANTITY

(SELECT SUM(SALES.QUAN)*PRICES.COST FROM SALES INNER JOIN PRICES ON PRICES.T_CODE = SALES.T_CODE) AS MARCH_VALUE

LEFT JOIN PRODUCTS.CODE
GROUP BY ST.NAME;

Please help me to construct correct SQL query for this.

Sales Products

Prices

Summary - this is what I want to get as the result of the query

2
  • What exactly is wrong with your current query?
    – esqew
    Jan 21, 2019 at 20:21
  • It doesn't sum the value for month correctly. For instance, price for apple was 5 USD for the period between 05/01/2018 and 09/01/2018. Any sum of the quantity sold between these two dates should be multiplied by 5 dollars. From 10/01/2018 price changed to 6 USD till 31/01/2018, let's say. Then any quantity sold between these dates should be multiplied by 6. This doesn't happen according to the current query. In other words, the wording of my current query doesn't account for the varying price for the each product within a month. Jan 21, 2019 at 20:51

1 Answer 1

1

SELECT SUM(SALES.QUAN)*PRICES.COST FROM SALES INNER JOIN PRICES ON PRICES.T_CODE = SALES.T_CODE) AS JANUARY_VALUE

You are not getting the output that you are missing a JOIN condition on that matches the date of the sale with the date of the price. Also, you would need to move the multiplication into the aggregate function.

Putting the price inside the aggregate function allows the calculation to work correctly.

Also, I believe that your query could probably be simplified by using conditional aggregation, as follows :

SELECT
    SUM(CASE WHEN S.DATE BETWEEN '01.01.2018' AND '31.01.2018' THEN S.QUAN ELSE 0 END) AS JANUARY_QUANTITY,
    SUM(CASE WHEN S.DATE BETWEEN '01.01.2018' AND '31.01.2018' THEN S.QUAN * P.COST ELSE 0 END) AS JANUARY_VALUE,
    SUM(CASE WHEN S.DATE BETWEEN '01.02.2018' AND '28.02.2018' THEN S.QUAN ELSE 0 END) AS FEBRUARY_QUANTITY,
    SUM(CASE WHEN S.DATE BETWEEN '01.02.2018' AND '28.02.2018' THEN S.QUAN * P.COST ELSE 0 END) AS FEBRUARY_VALUE,
    SUM(CASE WHEN S.DATE BETWEEN '01.03.2018' AND '31.03.2018' THEN S.QUAN ELSE 0 END) AS MARCH_QUANTITY,
    SUM(CASE WHEN S.DATE BETWEEN '01.03.2018' AND '31.03.2018' THEN S.QUAN * P.COST ELSE 0 END) AS MARCH_VALUE
FROM 
    SALES S
    LEFT JOIN PRICES P ON P.T_CODE = S.T_CODE AND P.DATE = S.DATE

With this technique, the table is scanned only once, and the results are then analyzed to feed the relevant columns in the resultset. This should efficiently replace your 6 subqueries.

0

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.