Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. SELECT sum(honoraria_amount)
  2. FROM table_1
  3.  
  4. table_id
  5. ----
  6. 1
  7. 39
  8. 42
  9.  
  10. SELECT sum(honoraria_amount)
  11. FROM table_x
  12.  
  13. CREATE VIEW union_table (id, name)
  14. AS ( SELECT honoraria_amount,... FROM foreign_table_1 UNION ALL
  15. SELECT honoraria_amount, ... FROM foreign_table_2
  16. ) ;
  17.  
  18. create or replace function sum_table(
  19. tid integer
  20. ) returns numeric as $$
  21. declare
  22. _out numeric;
  23. begin
  24. execute 'select sum(honoraria_amount) from table_' || tid::text || ';' into _out;
  25. return _out;
  26. end;
  27. $$ language plpgsql;
  28.  
  29. with
  30. __tables as(
  31. select unnest(array[1, 39, 42]) as table_id
  32. )
  33. select
  34. table_id,
  35. sum_table(table_id)
  36. from
  37. __tables
Add Comment
Please, Sign In to add comment