I have a Products
table where prices of products are updated every day.
eff_date | product_id | price |
---|---|---|
2022-11-25 | P1 | 150 |
2022-11-25 | P2 | 75.8 |
2022-11-25 | P3 | 2.9 |
2022-11-26 | P1 | 180.5 |
2022-11-26 | P2 | 77 |
2022-11-26 | P4 | 13.92 |
But sometimes not all products will have data for each date (like how p3 do not have data for 26th and p4 do not have data for 25th).
Consider today’s date is 26th then I want to compare today’s price with yesterday’s price and if difference is > 10% (price increased by 10% or more )then I want output like below:
eff_date | product_id | todays_price | yesterdays_price |
---|---|---|---|
2022-11-26 | P1 | 180.5 | 150 |
4
Answers
I have come up with the below query. Can someone please confirm if it's correct?
Adapting your date to the current_date,
you can do like Lamun said.
A CTE gets you all the data you need
fiddle
You can use a
CTE
which groups your data based on product_id and sorts by the date, then check if the current price >= previous price *1.1 to cover your condition the price must have increased by at least 10%.Try out: db<>fiddle
Try this:
Note that we are only including only those products whose price increased 10%