I have the following data:
And from this data, I would like to get the following table:
I tried the following code, but I get a lot of duplicates:
CREATE TABLE table_b AS ( SELECT a.ID, a.Date, a.Total, a.Total - coalesce(b.Total, 0) AS Daily FROM table_a a left outer JOIN table_a b ON (a.ID = b.ID AND b.Date < a.Date));
Thanks in advance:)
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
SELECT *, Total - COALESCE(LAG(Total) OVER (PARTITION BY id ORDER BY `date`), 0) Daily FROM src_table
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0