If I have ‘Table_A’ like above, is it possible to generate Table_B from it by using select sql only? I would like to keep the column ‘pc2’ and create the column ‘pc4’ to sum the values of ‘pc3’ and the value of ‘pc1’ from the consecutive row.
2
Use LEAD() window function to get the next value of pc1 of each row:
LEAD()
pc1
SELECT day, pc2, pc3 + LEAD(pc1, 1, 0) OVER (ORDER BY day) AS pc4 FROM Table_A;
If you want to create a new table Table_B:
Table_B
CREATE TABLE Table_B AS SELECT day, pc2, pc3 + LEAD(pc1, 1, 0) OVER (ORDER BY day) AS pc4 FROM Table_A;
You can try with the following SQL
SELECT a.day,a.pc1,(a.pc3+b.pc2) AS pc4 FROM TABLE_A AS a. TABLE_A AS b WHERE a.day=(b.day+1) ORDER BY a.day
Click here to cancel reply.
2
Answers
Use
LEAD()
window function to get the next value ofpc1
of each row:If you want to create a new table
Table_B
:You can try with the following SQL