Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table test1
- (
- id int primary key,
- ida int not null,
- dk int not null,
- s int not null
- );
- INSERT INTO public.test1 (id, ida, dk, s) VALUES (1, 1, 1, 20040);
- INSERT INTO public.test1 (id, ida, dk, s) VALUES (2, 1, 1, 1070);
- INSERT INTO public.test1 (id, ida, dk, s) VALUES (3, 1, 2, 11040);
- INSERT INTO public.test1 (id, ida, dk, s) VALUES (4, 2, 1, 12514);
- INSERT INTO public.test1 (id, ida, dk, s) VALUES (5, 2, 2, 1530);
- select *,
- (
- COALESCE((select sum(s) from test1 where dk = 1 and id <= t1.id), 0)
- -
- COALESCE((select sum(s) from test1 where dk = 2 and id <= t1.id), 0)
- )
- from test1 as t1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement