Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table test1
  2. (
  3.     id  int primary key,
  4.     ida int not null,
  5.     dk  int not null,
  6.     s   int not null
  7. );
  8.  
  9. INSERT INTO public.test1 (id, ida, dk, s) VALUES (1, 1, 1, 20040);
  10. INSERT INTO public.test1 (id, ida, dk, s) VALUES (2, 1, 1, 1070);
  11. INSERT INTO public.test1 (id, ida, dk, s) VALUES (3, 1, 2, 11040);
  12. INSERT INTO public.test1 (id, ida, dk, s) VALUES (4, 2, 1, 12514);
  13. INSERT INTO public.test1 (id, ida, dk, s) VALUES (5, 2, 2, 1530);
  14.  
  15. select *,
  16.        (
  17.                COALESCE((select sum(s) from test1 where dk = 1 and id <= t1.id), 0)
  18.                -
  19.                COALESCE((select sum(s) from test1 where dk = 2 and id <= t1.id), 0)
  20.            )
  21. from test1 as t1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement