合同更新.sql 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. --truncate table sqmdb_cost.hetong_new
  2. --insert into sqmdb_cost.hetong_new
  3. select
  4. ht1.*,
  5. case when ht1.nmonth=0 then 0 else ht1.cost_total/ht1.nmonth end as yuezujin,
  6. ht2.cost_center_name
  7. from
  8. (select
  9. *,
  10. finance_rmbmoney-finance_rmb_vatax as cost_total,--人民币金额-人民币增值税(原始“人民币不含增值税总金额”字段金额不全,有大部分0)
  11. extract(year from age(end_date::timestamp+'1 day',start_date::timestamp))*12+
  12. extract(month from age(end_date::timestamp+'1 day',start_date::timestamp))+
  13. extract(day from age(end_date::timestamp+'1 day',start_date::timestamp))/30 as nmonth
  14. from sqmdb_cost.hetong_jibenxinxi_e
  15. where vision_flag='当前版本'
  16. and arrair_status_name in ('履行中','审签中')
  17. and contract_status_name in ('履行中','审签中')
  18. and contract_cate_l1_code=20000
  19. and contract_cate_l3_name in ('支出类-网络运行及支撑成本-租赁','支出类-工程建设类-基站/设备用房租赁')
  20. and end_date::timestamp>date_trunc('month',now())) ht1
  21. left join
  22. (select period_id,contract_no_global,cost_center_name from
  23. (select period_id,contract_no_global,cost_center_name,
  24. row_number() over(partition by contract_no_global order by period_id::numeric desc) as rn
  25. from sqmdb_cost.hetong_yuti_e
  26. where contract_no_global is not null
  27. order by contract_no_global desc) cbzx
  28. where rn=1) ht2
  29. on ht1.contract_no_global=ht2.contract_no_global
  30. --合同无结束日期
  31. select
  32. *,
  33. extract(year from age(end_date::timestamp+'1 day',start_date::timestamp))*12+
  34. extract(month from age(end_date::timestamp+'1 day',start_date::timestamp))+
  35. extract(day from age(end_date::timestamp+'1 day',start_date::timestamp))/30 as nmonth
  36. from sqmdb_cost.hetong_jibenxinxi_e
  37. where vision_flag='当前版本'
  38. and arrair_status_name in ('履行中','审签中')
  39. and contract_status_name in ('履行中','审签中')
  40. and contract_cate_l1_code=20000
  41. and contract_cate_l3_name in ('支出类-网络运行及支撑成本-租赁','支出类-工程建设类-基站/设备用房租赁')
  42. and end_date is null