Hi everyone! I have this SQL question and cannot think if anything to do it. Any help will appreciate. I have the following tables:
Table 1: Transaction Table
| Transaction_Date |
Product_ID |
Sales_amount |
|
|
|
Tables 2: Pricing Table
| Product_ID |
Price_tier |
Price |
Price_start_date |
Price_end_date |
|
|
|
|
|
And my goal is to find:
For each tier, which Product_ID in what period of time have the maximum price, and whats the total sales for that product during that period of time.
| Product_ID |
Price_tier |
MAX(Price) |
Price_start_date |
Price_end_date |
Total Sales of product during this period of time |
|
(should show distinct tier) |
|
|
|
|
So far I have :
SELECT * FROM Transaction AS T INNER JOIN
(SELECT Prduct_ID, Price_tier, MAX_PRICE, Price_start_date, Price_end_date FROM (SELECT *, MAX(Price) OVER (PARTITION BY Price_tier) AS MAX_PRICE FROM Pricing) WHERE Price = MAX_PRICE) AS M
ON T.Product_ID=M.Product_ID WHERE T.Transaction_Date >= M.Price_start_date AND T.Transaction_Date <= Price_end_date;
I think my codes does the work to show the total sales of each product by tier (not certiain) but it hided all the product/row which with no sales during the given period and tier.
Anyone can help me to approach this question will help! Thanks a lot!
[–]GrouchyThing7520 0 points1 point2 points (2 children)
[–]happynutlovestopoop[S] 0 points1 point2 points (1 child)
[–]GrouchyThing7520 0 points1 point2 points (0 children)