大屏_看趋准.sql 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ---1-a.转供电+定额包干累计成本占比(%)
  2. ---chengben_quzhun_1_a_grid_202203.csv
  3. select t1.*,t2.city_code*100 as city_code,t3.area_code
  4. from
  5. (select smonth,city_name,area_name,
  6. sum(accrued_cost) as total_cost,
  7. sum(case when y.meter_type_name in ('12-01 | 转供手抄表','21-01 | 定额协议表','21-02 | 铁塔包干表') then accrued_cost else 0 end) as zgd_cost,
  8. sum(case when y.meter_type_name in ('12-01 | 转供手抄表','21-01 | 定额协议表','21-02 | 铁塔包干表') then accrued_cost else 0 end)/sum(accrued_cost) as zgd_cost_rate
  9. from sqmdb_cost.ytmx_new y
  10. group by smonth,city_name,area_name) t1
  11. left join
  12. (select distinct city_code,city_name from sqmdb_cost.dict_grid) t2
  13. on t1.city_name=t2.city_name
  14. left join
  15. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) t3
  16. on t1.city_name=t3.city_name and t1.area_name=t3.area_name
  17. order by t2.city_code,t3.area_code
  18. ---1-b.铁塔独享站成本占比
  19. ---chengben_quzhun_1_b_grid_202203.csv
  20. ----网格
  21. select t1.*,t2.city_code*100 as city_code,t3.area_code from
  22. (select sdate,city_operator,area_operator,
  23. sum(total_cost) as cost_total,
  24. sum(case when is_duxiang='独享' then total_cost else 0 end) as cost_du,
  25. sum(case when is_duxiang='独享' then total_cost else 0 end)/sum(total_cost) as cost_du_rate
  26. from sqmdb_cost.tower2 where sdate='202203'
  27. group by sdate,city_operator,area_operator) t1
  28. left join
  29. (select distinct city_code,city_name from sqmdb_cost.dict_grid) t2
  30. on t1.city_operator=t2.city_name
  31. left join
  32. (select distinct city_code,city_name,area_code,area_tower from sqmdb_cost.dict_grid) t3
  33. on t1.city_operator=t3.city_name and t1.area_operator=t3.area_tower
  34. order by t2.city_code,t3.area_code
  35. ---1-c.大于2倍铁塔单站租赁费三方站点占比
  36. ---chengben_quzhun_1_c_grid_202203.csv
  37. ----网格
  38. select t1.*,t2.city_code*100 as city_code,t3.area_code from
  39. (select sdate,city_name,area_name,
  40. sum(case when property_type='租用第三方' then 1 else 0 end) as cost_over2b_3rd_num,
  41. count(*) as station_num,
  42. sum(case when property_type='租用第三方' then 1 else 0 end)/count(*)::numeric as over_rate_3rd
  43. from(
  44. select *,
  45. cost_site.cost_tower/cost_cbzx.cbzx_cost_avg as cost_rate
  46. from
  47. (select station_sid,city_name,area_name,property_type,tower_code from sqmdb_cost.station3 s where s.property_type in ('租用铁塔','租用第三方')) st
  48. join
  49. (select sdate,site_addr_id,sum(total_cost) as cost_tower from sqmdb_cost.tower2 t group by sdate,site_addr_id) cost_site
  50. on st.tower_code=cost_site.site_addr_id
  51. join sqmdb_cost.dict_tower_cbzx dtc on st.tower_code=dtc.tower_code
  52. join
  53. (select sdate as cbzx_sdate,cbzx,avg(total_cost) as cbzx_cost_avg from(
  54. select a.sdate,b.cbzx,a.site_addr_id,a.total_cost
  55. from sqmdb_cost.tower2 a
  56. join sqmdb_cost.dict_tower_cbzx b
  57. on a.site_addr_id=b.tower_code) c
  58. group by sdate,cbzx) cost_cbzx
  59. on cost_site.sdate=cost_cbzx.cbzx_sdate and dtc.cbzx=cost_cbzx.cbzx
  60. where cost_site.cost_tower/cost_cbzx.cbzx_cost_avg>2
  61. and cost_site.sdate='202203') a
  62. group by sdate,city_name,area_name) t1
  63. left join
  64. (select distinct city_code,city_name from sqmdb_cost.dict_grid) t2
  65. on t1.city_name=t2.city_name
  66. left join
  67. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) t3
  68. on t1.city_name=t3.city_name and t1.area_name=t3.area_name
  69. order by t2.city_code,t3.area_code
  70. ---1-d.单载频载扇能耗
  71. ----同【比别人】第2项指标
  72. ---chengben_quzhun_1_d_city_202203.csv
  73. ---chengben_quzhun_1_d_grid_202203.csv
  74. ---1-e.基站单站租金合理性(局站维度)与【看异常】租赁费匹配顺序相同
  75. ---chengben_quzhun_1_e_grid_202203.csv
  76. ----网格
  77. ----判断标准:大于2倍铁塔成本中心平均租费
  78. select tb1.*,tb2.city_code*100 as city_code,tb3.area_code from
  79. (select sdate,city_name,area_name,
  80. count(*) as station_num,
  81. sum(case when cost_rate>2 then 1 else 0 end) as over2_num,
  82. sum(case when cost_rate>2 then 1 else 0 end)/count(*)::numeric as over2_rate
  83. from
  84. (select distinct t1.sdate,t1.city_name,t1.area_name,t1.station_sid,t1.cost_total as cost_station,
  85. t2.cbzx_cost_avg,t1.cost_total/t2.cbzx_cost_avg as cost_rate from
  86. (select
  87. a.station_sid,a.station_sname,a.city_name,a.area_name,a.property_type,a.station_level,a.tower_code,
  88. c.yuezujin as hetong_yuezujin,--合同系统月租金(由合同总月份数和总金额计算得出)
  89. c.cname,c.cfname,c.cbzx as hetong_cbzx,c.sid,c.sname,
  90. d.sdate,e.cbzx as tower_cbzx,d.service_tracking_id,d.site_addr_id,d.stype,d.is_duxiang,d.total_cost as tower_cost,
  91. (coalesce(c.yuezujin,0)+coalesce(d.total_cost,0)) as cost_total,--总费用(合同月租金使用合同系统值)
  92. case when c.cbzx is null then e.cbzx else c.cbzx end as cbzx
  93. from sqmdb_cost.station3 a
  94. left join sqmdb_cost.station_hetong b on a.station_sid=b.station_sid
  95. left join sqmdb_cost.hetong2 c on c.sid=b.hetong_sid||E'\t'
  96. left join sqmdb_cost.tower2 d on a.tower_code=d.site_addr_id
  97. left join sqmdb_cost.dict_tower_cbzx e on a.tower_code=e.tower_code) t1
  98. left join
  99. --本单位铁塔单站租赁费
  100. (select sdate,cbzx,avg(total_cost) as cbzx_cost_avg from(
  101. select a.sdate,b.cbzx,a.site_addr_id,a.total_cost
  102. from sqmdb_cost.tower2 a
  103. left join sqmdb_cost.dict_tower_cbzx b
  104. on a.site_addr_id=b.tower_code) c
  105. group by sdate,cbzx) t2
  106. on t1.cbzx=t2.cbzx
  107. where property_type in ('租用铁塔','租用第三方')
  108. and t1.sdate='202203'
  109. and t2.sdate='202203') tmp
  110. group by sdate,city_name,area_name) tb1
  111. left join
  112. (select distinct city_code,city_name from sqmdb_cost.dict_grid) tb2
  113. on tb1.city_name=tb2.city_name
  114. left join
  115. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) tb3
  116. on tb1.city_name=tb3.city_name and tb1.area_name=tb3.area_name
  117. order by tb2.city_code,tb3.area_code
  118. ---2.有合同无局站
  119. ---chengben_quzhun_2_city_202203.csv
  120. with vt as (select a.sid,a.sname,a.cname,a.cfname,a.cbzx,a.yuezujin,
  121. b.city_name,b.city_code,b.district_code,b.hetong_sid,b.station_sid as station_sid_i,
  122. c.station_sid as station_sid_s,
  123. (case
  124. when b.hetong_sid is not null and c.station_sid is not null then '有合同有局站'--'合同系统有,资源系统没'
  125. when b.hetong_sid is null or (b.hetong_sid is not null and c.station_sid is null) then '有合同无局站'--'合同系统有,资源系统没有'
  126. else '其他'
  127. end) as check_result
  128. from sqmdb_cost.hetong2 a
  129. left join sqmdb_cost.station_hetong b on a.sid=b.hetong_sid||E'\t'
  130. left join sqmdb_cost.station3 c on b.station_sid=c.station_sid)
  131. select '202203' as sdate,t1.*,t2.city_code*100 as city_code from
  132. (select substr(cname,5,length(cname)-7) as city,sid,sname,yuezujin as cost_hetong,cbzx
  133. from vt where check_result='有合同无局站') t1
  134. left join
  135. (select distinct city_code,city_sname from sqmdb_cost.dict_grid) t2
  136. on t1.city=t2.city_sname
  137. where t2.city_code is not null
  138. order by t2.city_code,t1.cost_hetong desc
  139. ---3.有能耗无局站
  140. ---chengben_quzhun_3_city_202203.csv
  141. with vt as (select nh.smonth,nh.city_name,nh.build_code,nh.build_name,nh.accrued_cost,nh.cost_unit_name,s.station_sid,
  142. (case when s.station_sid is not null then '有能耗有局站' else '有能耗无局站' end) as check_result
  143. from
  144. (select smonth,city_name,area_name,build_code_short,build_code,build_name,cost_unit_name,accrued_cost from sqmdb_cost.ytmx_new) nh
  145. left join
  146. (select distinct station_sid,nh_sid_short from sqmdb_cost.station_nenghao) sy
  147. on nh.build_code_short=sy.nh_sid_short
  148. left join
  149. (select station_sid,station_sname from sqmdb_cost.station3) s
  150. on sy.station_sid=s.station_sid
  151. where nh.smonth='202203')
  152. select t1.*,t2.city_code*100 as city_code from
  153. (select smonth,city_name,build_code,build_name,accrued_cost,cost_unit_name from vt where check_result='有能耗无局站') t1
  154. left join
  155. (select distinct city_code,city_name from sqmdb_cost.dict_grid) t2
  156. on t1.city_name=t2.city_name
  157. order by t2.city_code,t1.cost_unit_name
  158. ---4.有铁塔订单无局站
  159. ---chengben_quzhun_4_city_202203.csv
  160. with vt as (select t.sdate,t.city_operator,t.site_addr_id,dtc.tower_name,t.total_cost,dtc.cbzx,tf.fzd_id,
  161. (case when tf.fzd_id is not null then '有铁塔订单有局站' else '有铁塔订单无局站' end) as check_result
  162. from
  163. (select sdate,city_operator,site_addr_id,sum(total_cost) as total_cost from sqmdb_cost.tower2
  164. group by sdate,city_operator,site_addr_id) t
  165. left join sqmdb_cost.dict_tower_cbzx dtc
  166. on t.site_addr_id=dtc.tower_code
  167. left join sqmdb_cost.tower_fzd tf
  168. on t.city_operator=tf.city_name and t.site_addr_id||E'\t'=tf.tower_code
  169. where t.sdate='202203')
  170. select t1.*,t2.city_code*100 as city_code from
  171. (select sdate,city_operator,site_addr_id,tower_name,total_cost,cbzx from vt where check_result='有铁塔订单无局站') t1
  172. left join
  173. (select distinct city_code,city_name from sqmdb_cost.dict_grid) t2
  174. on t1.city_operator=t2.city_name
  175. order by t2.city_code,t1.tower_name,t1.total_cost desc