Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. create table dwh_budget_kgntv as
  2. with filter_ as
  3. (select l.id lid,
  4. cc.id cid
  5. from lots l
  6. left join cm_contracts cc
  7. on l.id = cc.lot_id
  8. join procedure_steps ps
  9. on ps.id = l.current_step and ps.actual = true
  10. join vocab_procedure_steps vps
  11. on vps.pseudo::text = ps.step_id::text and vps.actual = true
  12. join site.naumen_kgntv_stages nks
  13. on nks.kgntv_id = vps.id
  14. where vps.id != 93 and nks.naumen_id in (5, 6, 11, 12)
  15. )
  16. select pa.id as accountid,
  17. ca.id as contragentsid,
  18. pvtp.name as budgetyear,
  19. gea.exp_account as exp_account,
  20. gea.section_code as section_code,
  21. (gea.grbs_code
  22. ||gea.section_code
  23. ||gea.article_code
  24. ||gea.e_code
  25. ||gea.kosgu) as kbk,
  26. gea.article_code as article_code,
  27. gla.name as article_name,
  28. gea.kosgu as kosgu,
  29. glk.name as kosgu_name,
  30. pvbt.description as budget_name,
  31. gea.e_code as e_code,
  32. gea.fund_code as fund_code,
  33. pby.sum as allocated_amount,
  34. planlot.planlot_amount,
  35. pby.sum - coalesce(planlot.planlot_amount, 0) restlot,
  36. pby.sum - coalesce(plancont.plancont_amount, 0) restcontract
  37. from contragents as ca
  38. join po_budget as pb
  39. on pb.contragent_id = ca.id
  40. join po_account as pa
  41. on pa.po_budget_id = pb.id
  42. join po_exp_account as pea
  43. on pea.id = pa.po_exp_account_id
  44. join gpo_po_exp_account as gpea
  45. on pea.id = gpea.po_exp_code_id
  46. join gpo_exp_account as gea
  47. on gea.id = gpea.gpo_exp_account_id
  48. join gpo_list_article as gla
  49. on gla.article_code = gea.article_code
  50. join po_vocab_budget_types as pvbt
  51. on pvbt.code = pea.budget_type
  52. join po_vocab_time_periods as pvtp
  53. on pvtp.id = pb.po_period_id
  54. join gpo_list_kosgu as glk
  55. on glk.kosgu_code = gea.kosgu
  56. join po_budget_year as pby
  57. on pby.id = pa.id
  58. left join
  59. (select sum(pf.amount) as planlot_amount,
  60. pf.po_account_id
  61. from po_finances pf
  62. where pf.lot_id in
  63. (select lid
  64. from filter_
  65. )
  66. group by po_account_id
  67. )planlot
  68. on planlot.po_account_id = pa.id
  69. left join
  70. (select sum(ccf.amount) as plancont_amount,
  71. ccf.po_account_id
  72. from cm_contract_finances ccf
  73. where amount is not null and type = 1 and contract_id in
  74. (select cid
  75. from filter_
  76. )
  77. group by po_account_id
  78. )plancont
  79. on plancont.po_account_id = pa.id
  80. where pb.actual = true and pby.actual = true and pea.actual = true and
  81. gla.actual = true and glk.actual = true and pvbt.actual = true and ca.actual
  82. is true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement