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

Find price changes over time

I have a table with these columns: id, sku, price, date

CREATE TABLE price_log (
  id int NOT NULL  PRIMARY KEY,
  sku text NOT NULL,
  date date NOT NULL,
  price real NOT NULL,
  CONSTRAINT date_sku UNIQUE (sku,date)
)

I want to get a report of change price in overtime.

My desired output is:

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

sku     old_price    new_price    change_date
A          10           11         2022-01-03
B          1            5.99       2022-01-02
B          5.99         3.5        2022-01-03
B          3.5          12         2022-01-04

Right now the best I could do is to use LAG() function:

WITH cte AS (
 SELECT sku, price, MAX(date) AS date
 FROM price_log
 GROUP BY sku, price
)
SELECT sku, price, date,
LAG(price,1) OVER (
    PARTITION BY sku
    ORDER BY date
) AS old_price
FROM cte

dbfiddle

>Solution :

There are N ways to do that. For example one would be to use lateral:

select p1.sku, t.old_price, p1.price as new_price, p1.date as change_Date
from price_log p1, 
lateral (select price from price_log p2 
         where p1.sku = p2.sku and
               p1.date > p2.date
         order by date desc
         limit 1) t(old_price);

DbFiddle demo

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