Skip to content
Advertisement

Add column with yesterday sale information on a daily sales database

I’m working with a database that contains daily sales information of different products and stores.

i want to add a column with one day ago unit sales information

i’ve got to be honest, i don’t know if sql is able to achieve this. If not, could you please recommend a way? considering there are over thousands of records.

Advertisement

Answer

You are probably looking for lag():

Alternatively, you can use a left join with date arithmetic. That varies by database, but the standard would be:

These are not exactly equivalent. The first will get the previous day in the data. The second will get the previous day — with NULL for the day after missing days.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement