大屏_看异常.sql 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. --铁塔成本
  2. with chengben as (
  3. select sdate,city_operator,city_service,city_site,stype,is_duxiang,avg(total_cost) as avgcost from sqmdb_cost.tower2
  4. where total_cost>0
  5. group by sdate,city_operator,city_service,city_site,stype,is_duxiang)
  6. select a.*,d.cbzx,chengben.avgcost,
  7. a.total_cost/chengben.avgcost as cost_rate
  8. from sqmdb_cost.tower2 a
  9. left join chengben on chengben.sdate=a.sdate and chengben.city_operator=a.city_operator and chengben.stype=a.stype
  10. and chengben.is_duxiang=a.is_duxiang
  11. left join sqmdb_cost.dict_tower_cbzx d on a.site_addr_id=d.tower_code;
  12. --局站租金
  13. with station_cost as (
  14. select a.city_code,b.dicttext as city_name,a.area_code,c.dicttext as area_name,a.station_sid,e.cbzx,d.total_cost as yuezujin,'铁塔' as stype from sqmdb_cost.station2 a
  15. join sqmdb_cost.tower2 d on a.tower_code=d.site_addr_id
  16. left join sqmdb_cost.dict b on b.dictid=a.city_code::varchar
  17. left join sqmdb_cost.dict c on c.dictid=a.area_code::varchar
  18. left join sqmdb_cost.dict_tower_cbzx e on a.tower_code=e.tower_code
  19. union
  20. select a.city_code,b.dicttext as city_name,a.area_code,c.dicttext as area_name,d.station_sid,e.cbzx,d.yuezujin,'合同' as stype
  21. from sqmdb_cost.station_hetong d
  22. left join sqmdb_cost.station2 a on a.station_sid=d.station_sid
  23. left join sqmdb_cost.dict b on b.dictid=a.city_code::varchar
  24. left join sqmdb_cost.dict c on c.dictid=a.area_code::varchar
  25. left join sqmdb_cost.hetong2 e on e.sid=d.hetong_sid||E'\t'
  26. )
  27. select city_code,city_name,area_code,area_name,station_sid,sum(yuezujin)
  28. from station_cost
  29. group by city_code,city_name,area_code,area_name,station_sid
  30. --租赁费(chengben_exception_1_grid_202203.csv)
  31. ----横向匹配 局站表a <- 合同索引b <- 合同系统c <- 铁塔全量表d
  32. select tb1.*,tb2.city_code*100 as city_code,tb3.area_code from
  33. (select
  34. distinct
  35. t1.sdate,
  36. t1.city_name,
  37. t1.area_name,
  38. t1.station_sid,
  39. t1.station_sname,
  40. t1.property_type,
  41. t1.sid as hetong_sid,
  42. t1.hetong_yuezujin as cost_hetong,
  43. t1.tower_code,
  44. t1.tower_cost as cost_tower,
  45. t1.cost_total,
  46. t2.cbzx,
  47. t2.cbzx_cost_avg,
  48. t1.cost_total/t2.cbzx_cost_avg as cost_rate
  49. from (
  50. select
  51. a.station_sid,a.station_sname,a.city_name,a.area_name,a.property_type,a.station_level,a.tower_code,
  52. c.yuezujin as hetong_yuezujin,--合同系统月租金(由合同总月份数和总金额计算得出)
  53. c.cname,c.cfname,c.cbzx as hetong_cbzx,c.sid,c.sname,
  54. 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,
  55. (coalesce(c.yuezujin,0)+coalesce(d.total_cost,0)) as cost_total,--总费用(合同月租金使用合同系统值)
  56. case when c.cbzx is null then e.cbzx else c.cbzx end as cbzx
  57. from sqmdb_cost.station3 a
  58. left join sqmdb_cost.station_hetong b on a.station_sid=b.station_sid
  59. left join sqmdb_cost.hetong2 c on c.sid=b.hetong_sid||E'\t'
  60. left join sqmdb_cost.tower2 d on a.tower_code=d.site_addr_id
  61. left join sqmdb_cost.dict_tower_cbzx e on a.tower_code=e.tower_code) t1
  62. left join
  63. --本单位铁塔单站租赁费
  64. (select sdate,cbzx,avg(total_cost) as cbzx_cost_avg from(
  65. select a.sdate,b.cbzx,a.site_addr_id,a.total_cost
  66. from sqmdb_cost.tower2 a
  67. left join sqmdb_cost.dict_tower_cbzx b
  68. on a.site_addr_id=b.tower_code) c
  69. group by sdate,cbzx) t2
  70. on t1.cbzx=t2.cbzx
  71. where property_type in ('租用铁塔','租用第三方')
  72. and t1.cost_total/t2.cbzx_cost_avg>1.5
  73. and t1.sdate='202203'
  74. and t2.sdate='202203') tb1
  75. left join
  76. (select distinct city_code,city_name from sqmdb_cost.dict_grid) tb2
  77. on tb1.city_name=tb2.city_name
  78. left join
  79. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) tb3
  80. on tb1.city_name=tb3.city_name and tb1.area_name=tb3.area_name
  81. order by tb2.city_code,tb3.area_code
  82. --电费单价(新)使用局站级的电费预提明细
  83. --(chengben_exception_2_grid_202203.csv)
  84. select yn.smonth,yn.city_name,yn.area_name,s.station_sid,s.station_sname,yn.meter_type_name,yn.accrued_cost,yn.accrued_power,
  85. case when accrued_power=0 then 0 else yn.accrued_cost/yn.accrued_power end as eprice,yn.cost_unit_name,
  86. dg1.city_code*100 as city_code,dg2.area_code
  87. from sqmdb_cost.ytmx_new yn
  88. left join sqmdb_cost.station_nenghao sn on yn.build_code_short=sn.nh_sid_short
  89. left join sqmdb_cost.station3 s on sn.station_sid=s.station_sid
  90. left join sqmdb_cost.dict_grid dg on yn.city_name=dg.city_name and yn.area_name=dg.area_name
  91. left join (select distinct city_code,city_name from sqmdb_cost.dict_grid) dg1
  92. on yn.city_name=dg1.city_name
  93. left join
  94. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) dg2
  95. on yn.city_name=dg2.city_name and yn.area_name=dg2.area_name
  96. where yn.meter_type_name in ('12-01 | 转供手抄表','21-01 | 定额协议表','21-02 | 铁塔包干表')
  97. and s.station_sid is not null
  98. and (case when accrued_power=0 then 0 else yn.accrued_cost/yn.accrued_power end)>0.8
  99. and yn.smonth='202203'
  100. order by dg1.city_code,dg2.area_code
  101. --select distinct meter_type_name from sqmdb_cost.ytmx_new yn
  102. --局站功耗
  103. ---rru先关联机房 关联局站
  104. select *,ri.room_sid from
  105. (select rru_id,bbu_id,city_name,district_name,unnest(string_to_array(cell_oid,'|')) as cid,room from sqmdb_cost.rru where room is not null) r
  106. left join sqmdb_cost.roomid ri
  107. on r.room=ri.room_id
  108. where ri.room_sid is not null
  109. select *,ri.room_sid from
  110. (select bbu_id,city_name,district_name,vendor,site_id,room,net_type from sqmdb_cost.bbu) b
  111. left join sqmdb_cost.roomid ri
  112. on b.room=ri.room_id
  113. where ri.room_sid is not null
  114. ---(RRU先和BBU关联,再通过BBU关联到机房,再关联到局站)
  115. select * from
  116. (select r2.city_code,r2.area_code,r.city_name,r.district_name,r2.station_sid,r2.station_name,r.cid,
  117. (regexp_split_to_array(r.cid,'\.'))[2] as enbid,
  118. (regexp_split_to_array(r.cid,'\.'))[3] as cellid
  119. from
  120. (select rru_id,bbu_id,city_name,district_name,unnest(string_to_array(cell_oid,'|')) as cid,room from sqmdb_cost.rru) r
  121. join
  122. (select bbu_id,room from sqmdb_cost.bbu) b
  123. on r.bbu_id=b.bbu_id
  124. join sqmdb_cost.roomid ri
  125. on b.room=ri.room_id
  126. join sqmdb_cost.room r2
  127. on ri.room_sid=r2.room_sid) a
  128. left join
  129. (select smonth,siteid,ci,sum(nh_kwh) as nh_kwh from sqmdb_cost.nenghao_rru group by smonth,siteid,ci) b
  130. on a.enbid=b.siteid and a.cellid=b.ci
  131. --在用(专业网管先算出本站所有RRU功耗,通过站号关联BBU,再关联机房-局站-预提明细)
  132. --(chengben_exception_3_grid_202203.csv)
  133. select t1.smonth,t1.city_code*100 as city_code,t1.area_code,t2.station_sid,t2.station_name,
  134. t1.nh_total,t2.accrued_power,t2.cost_unit_name
  135. from
  136. (select nr.smonth,r2.city_code,r2.area_code,r2.station_sid,sum(nh_rru+nh_bbu) as nh_total from
  137. (select bbu_id,site_id,(regexp_split_to_array(site_id,'\.'))[2] as enbid,room from sqmdb_cost.bbu) b
  138. join
  139. (select smonth,siteid,sum(nh_kwh) as nh_rru from sqmdb_cost.nenghao_rru group by smonth,siteid) nr
  140. on b.enbid=nr.siteid
  141. join
  142. (select smonth,siteid,sum(nh_kwh) as nh_bbu from sqmdb_cost.nenghao_bbu group by smonth,siteid) nb
  143. on nr.smonth=nb.smonth and nr.siteid=nb.siteid
  144. join sqmdb_cost.roomid ri
  145. on b.room=ri.room_id
  146. join sqmdb_cost.room r2
  147. on ri.room_sid=r2.room_sid
  148. group by nr.smonth,r2.city_code,r2.area_code,r2.station_sid) t1--专业网管能耗
  149. join
  150. (select smonth,station_sid,station_name,cost_unit_name,sum(accrued_cost) as accrued_cost,sum(accrued_power) as accrued_power from
  151. (select yn.smonth,sn.station_sid,station_name,yn.accrued_cost,yn.accrued_power,yn.cost_unit_name from sqmdb_cost.ytmx_new yn
  152. join sqmdb_cost.station_nenghao sn
  153. on yn.build_code_short=sn.nh_sid_short) tp group by smonth,station_sid,station_name,cost_unit_name) t2 --能耗表能耗
  154. on t1.smonth=t2.smonth and t1.station_sid=t2.station_sid