合同能耗校验.sql 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. --合同校验
  2. --有一个合同对应多个局站,例 CU12-1308-2012-000802-2,校验过程中只保留一条避免结果重复
  3. with check_detail as (
  4. select (case
  5. when strpos(cname,'石家庄')>0 then '石家庄市'
  6. when strpos(cname,'唐山')>0 then '唐山市'
  7. when strpos(cname,'秦皇岛')>0 then '秦皇岛市'
  8. when strpos(cname,'邯郸')>0 then '邯郸市'
  9. when strpos(cname,'邢台')>0 then '邢台市'
  10. when strpos(cname,'保定')>0 then '保定市'
  11. when strpos(cname,'张家口')>0 then '张家口市'
  12. when strpos(cname,'承德')>0 then '承德市'
  13. when strpos(cname,'沧州')>0 then '沧州市'
  14. when strpos(cname,'廊坊')>0 then '廊坊市'
  15. when strpos(cname,'衡水')>0 then '衡水市'
  16. when strpos(cname,'雄安')>0 then '雄安新区'
  17. when strpos(cname,'本部')>0 then '河北省本部'
  18. else cname
  19. end
  20. ) as city_name,*,
  21. (case when sh.hetong_sid is null then '有合同无局站' else '有合同有局站' end) as check_result
  22. from
  23. (select sid,sname,cname,cbzx from sqmdb_cost.hetong2) h
  24. left join
  25. (select hetong_sid,station_sid from
  26. (select hetong_sid,station_sid,
  27. row_number() over(partition by hetong_sid order by station_sid desc) as rn
  28. from sqmdb_cost.station_hetong) ht_rank
  29. where rn=1) sh
  30. on h.sid=sh.hetong_sid||E'\t'
  31. --left join
  32. --(select station_sid from sqmdb_cost.station3) s
  33. --on sh.station_sid=s.station_sid
  34. )
  35. select city_name as 地市,
  36. count(*) as 合同总数,
  37. sum(case when check_result='有合同有局站' then 1 else 0 end) as 合同关联数,
  38. sum(case when check_result='有合同无局站' then 1 else 0 end) as 合同未关联数,
  39. sum(case when check_result='有合同有局站' then 1 else 0 end)/count(*)::numeric as 合同关联率
  40. from check_detail
  41. group by city_name
  42. order by sum(case when check_result='有合同有局站' then 1 else 0 end)/count(*)::numeric desc
  43. --能耗校验
  44. --有一个能耗ID对应多个局站,例 2039913,校验过程中只保留一条避免结果重复
  45. with check_detail as (
  46. select *,
  47. (case when sn.nh_sid_short is null then '有能耗无局站' else '有能耗有局站' end) as check_result
  48. from
  49. (select city_name,area_name,build_code_short,cost_unit_name
  50. from sqmdb_cost.ytmx_new where smonth='202203' and build_code_short is not null) yn
  51. left join
  52. (select nh_sid_short,station_sid from
  53. (select nh_sid_short,station_sid,
  54. row_number() over(partition by nh_sid_short order by station_sid desc) as rn
  55. from sqmdb_cost.station_nenghao) nh_rank
  56. where rn=1) sn
  57. on yn.build_code_short=sn.nh_sid_short
  58. --left join
  59. --(select station_sid from sqmdb_cost.station3) s
  60. --on sn.station_sid=s.station_sid
  61. )
  62. select city_name as 地市,
  63. count(*) as 能耗总数,
  64. sum(case when check_result='有能耗有局站' then 1 else 0 end) as 能耗关联数,
  65. sum(case when check_result='有能耗无局站' then 1 else 0 end) as 能耗未关联数,
  66. sum(case when check_result='有能耗有局站' then 1 else 0 end)/count(*)::numeric as 能耗关联率
  67. from check_detail
  68. group by city_name
  69. order by sum(case when check_result='有能耗有局站' then 1 else 0 end)/count(*)::numeric desc