I have a bq table that recieves data quarter to date per id
id | value | date |
---|---|---|
1 | 200 | 02/11/2022 |
2 | 70 | 02/11/2022 |
3 | 120 | 02/11/2022 |
1 | 150 | 01/11/2022 |
2 | 50 | 01/11/2022 |
3 | 100 | 01/11/2022 |
So each id got the cumulative data of the quarter I need to create a view that takes each day's value minus the day's before per id
for id 1 (02/11 value minus 01/11 value, ...etc)
so the output should be like this
id | value | date |
---|---|---|
1 | 50 | 02/11/2022 |
2 | 20 | 02/11/2022 |
3 | 20 | 02/11/2022 |
1 | 150 | 01/11/2022 |
2 | 50 | 01/11/2022 |
3 | 100 | 01/11/2022 |
any help is really appreciated
CodePudding user response:
you might consider below query.
SELECT ID, date,
value - LEAD(value, 1, 0) OVER (
PARTITION BY id ORDER BY UNIX_DATE(PARSE_DATE('%d/%m/%Y', date)) DESC
) AS new_value
FROM sample_data;
Query results