Skip to content
Advertisement

SQL – Get previous amounts for each day

I’m trying to write a query that is sort of like a running total but not really. I want to get the previous weight (kg) and keep outputting that for each day until another weight (kg) is recorded then continue to output that until next weight recorded. Below is example of what I’m trying to accomplish (see KG column).

Current results:

Desired Results:

Advertisement

Answer

In standard SQL, you would use lag() with the ignore nulls option:

Not all databases support ignore nulls. But it is standard SQL and you haven’t specified the database you are using.

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