I have a table with this data:
create table my_table (
month varchar(2),
services integer null,
s_registered integer,
s_terminated integer
);
insert into my_table(month, services, s_registered, s_terminated) values
('01', 395, 14, 14),
('02', null, 5, 9),
('03', null, 19, 15),
('04', null, 30, 11),
('05', null, 13, 15),
('06', null, 8, 11),
('07', null, 10, 17),
('08', null, 13, 17),
('09', null, 5, 10),
('10', null, 10, 19),
('11', null, 7, 22),
('12', null, 18, 13);
You can see the services
column and I need a query to select the result with the calculated services
in each row.
The services
column is equal to services + s_registered - s_termination
of the previous row.
我try 使用Window CTE Lag()函数,但它只起作用一次.它正确地接受了第一个services
列,但在下一次迭代中,LAG将该列视为NULL.
我觉得这个问题可以用递归CTE来解决,你有什么 idea 吗?:)