存储过程表.sql 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. --网格端口数天粒度汇聚月粒度,每月初执行(不要执行2022年4月之前的月份,前期数据没有,执行会导致现有表前期月份数据删除,如需恢复,用202204的数据改日期后重新导入)
  2. --call sqmdb_cost.kuandai_ftth_month('202207');
  3. --省内成本入库后计算当月值
  4. call sqmdb_cost.chengben_shengnei_m('202207');
  5. --端口数、用户数、皮长公里中间表
  6. call sqmdb_cost.jt_number_b10('202207');--每次执行去年同期和本月
  7. call sqmdb_cost.jt_number_city('202207');
  8. --更新铁塔成本中心字典(从铁塔预提表生成字典数据)
  9. call sqmdb_cost.update_dict_tower_cbzx();
  10. --单载频载扇能耗
  11. call sqmdb_cost.rpt_dzpzs_cost_b10('202207');--每次执行去年同期和本月
  12. call sqmdb_cost.rpt_dzpzs_cost_city('202207');
  13. call sqmdb_cost.rpt_dzpzs_cost_grid('202207');
  14. call sqmdb_cost.rpt_dzpzs_cost_prov('202207');
  15. --单端口能耗
  16. call sqmdb_cost.rpt_dport_cost_b10('202207');--每次执行去年同期和本月
  17. call sqmdb_cost.rpt_dport_cost_city('202207');
  18. call sqmdb_cost.rpt_dport_cost_grid('202207');
  19. --铁塔单站租赁费
  20. call sqmdb_cost.rpt_tower_cost_city('202207');
  21. call sqmdb_cost.rpt_tower_cost_grid('202207');
  22. --6个报表
  23. --call sqmdb_cost.rpt_baobiao_1_dzpz('202207');
  24. --call sqmdb_cost.rpt_baobiao_2_ddk('202207');
  25. --call sqmdb_cost.rpt_baobiao_3_dzzl('202207');
  26. --call sqmdb_cost.rpt_baobiao_4_cgzf('202207');
  27. --call sqmdb_cost.rpt_baobiao_5_cgdf('202207');
  28. --call sqmdb_cost.rpt_baobiao_6_cgnh('202207');
  29. call sqmdb_cost.rpt_baobiao_base('202207');
  30. --校验-只保留当期数据!!!
  31. --有合同无局站
  32. --call sqmdb_cost.err_has_ht_no_station('202207');
  33. --有能耗无局站
  34. --call sqmdb_cost.err_has_nh_no_station('202207');
  35. --有铁塔订单无局站
  36. --call sqmdb_cost.err_has_tw_no_station('202207');
  37. --又有铁塔又有第三方(偶尔慢,执行200s左右)
  38. --call sqmdb_cost.err_station_has_tw_3rd('202207');
  39. call sqmdb_cost.err_base('202207');
  40. --rpt_dzpzs_cost_prov
  41. --单载频载扇能耗-全省
  42. --create table sqmdb_cost.rpt_dzpzs_cost_prov as
  43. select fz.smonth,fz.sname,fz.cost_yw,fz.cost_hexin,
  44. (fz.cost_yw+fz.cost_hexin) as cost_total,
  45. fm.zps_num_with5g,
  46. fm.zps_num_no5g,
  47. case when fm.zps_num_with5g=0 then 0 else (fz.cost_yw+fz.cost_hexin)/fm.zps_num_with5g/4 end as cost_zps_with5g,
  48. case when fm.zps_num_no5g=0 then 0 else (fz.cost_yw+fz.cost_hexin)/fm.zps_num_no5g/4 end as cost_zps_no5g
  49. from
  50. (select '202204' as smonth,sname,
  51. sum(column4+column5)/10000 as cost_yw,
  52. sum(column19)/10000 as cost_hexin
  53. from sqmdb_cost.report50
  54. where period_type='M' and sname ='河北' and smonth between '202201' and '202204' group by sname) fz
  55. join
  56. (select a.city_name,
  57. (a.zps_total+b.zps_total)/10000/2 as zps_num_with5g,
  58. (a.zps_total_no5g+b.zps_total_no5g)/10000/2 as zps_num_no5g
  59. from
  60. (select '河北' as city_name,(zps_total-coalesce(zps_5g,0)) as zps_total_no5g,zps_total from sqmdb_cost.zpzs_city where smonth='202112' and city_name='全省')a
  61. join
  62. (select '河北' as city_name,(zps_total-coalesce(zps_5g,0)) as zps_total_no5g,zps_total from sqmdb_cost.zpzs_city where smonth='202204' and city_name='全省')b
  63. on a.city_name=b.city_name) fm
  64. on fz.sname=fm.city_name
  65. --rpt_dzpzs_cost_city
  66. --单载频载扇能耗-地市
  67. --create table sqmdb_cost.rpt_dzpzs_cost_city as
  68. select fz.smonth,fz.city_sname,fz.cost_yw,fz.cost_hexin,
  69. (fz.cost_yw+fz.cost_hexin) as cost_total,
  70. fm.zps_num_with5g,
  71. fm.zps_num_no5g,
  72. case when fm.zps_num_with5g=0 then 0 else (fz.cost_yw+fz.cost_hexin)/fm.zps_num_with5g/4 end as cost_zps_with5g,
  73. case when fm.zps_num_no5g=0 then 0 else (fz.cost_yw+fz.cost_hexin)/fm.zps_num_no5g/4 end as cost_zps_no5g,
  74. fz.city_code*100 as city_code
  75. from
  76. (select a.smonth,dg.city_sname,dg.city_code,a.cost_yw,a.cost_hexin from
  77. (select '202204' as smonth,sname,
  78. sum(column4+column5)/10000 as cost_yw,
  79. sum(column19)/10000 as cost_hexin
  80. from sqmdb_cost.report50
  81. where period_type='M'
  82. and sname in('石家庄_河北','唐山_河北','秦皇岛_河北','邯郸_河北','邢台_河北','保定_河北','张家口_河北','承德_河北','廊坊_河北','沧州_河北','衡水_河北','雄安_河北')
  83. and smonth between '202201' and '202204' group by sname) a
  84. join (select distinct city_code,city_sname,city_pro from sqmdb_cost.dict_grid) dg
  85. on a.sname=dg.city_pro) fz
  86. join
  87. (select
  88. city_name,
  89. zps_total_with5g as zps_num_with5g,
  90. zps_total_no5g as zps_num_no5g
  91. from sqmdb_cost.jt_number_city where month_id='202204') fm
  92. on fz.city_sname=fm.city_name
  93. order by fz.city_code
  94. --单载频载扇能耗--网格
  95. --drop table sqmdb_cost.rpt_dzpzs_cost_grid_1
  96. --create table sqmdb_cost.rpt_dzpzs_cost_grid_1 as
  97. select fm.smonth,dcg.city_name,dcg.grid_name,fz.cost_yw,fz.cost_hexin,
  98. (fz.cost_yw+fz.cost_hexin) as cost_total,
  99. fm.zps_num_with5g,
  100. case when fm.zps_num_with5g=0 then 0 else (fz.cost_yw+fz.cost_hexin)/fm.zps_num_with5g/4 end as cost_zps_with5g,
  101. dcg.city_code,dcg.grid_code
  102. from
  103. (select jt.smonth,jt.city_name,jt.grid_name,jt.cost_yw,jt.cost_hexin,dgc.grid_code from
  104. (select smonth,city_name_short(city_name) as city_name,grid_name,(column4+column5) as cost_yw,column19 as cost_hexin
  105. from sqmdb_cost.report49 where period_type='Y' and smonth='202204')jt
  106. left join sqmdb_cost.dict_grid_code dgc on jt.city_name=dgc.city_name and trim(jt.grid_name)=trim(dgc.grid_name)
  107. where dgc.grid_code is not null) fz
  108. join
  109. (select z1.smonth,z1.city_name as city_name,z1.grid_name,
  110. z2.zps_num_total as zps_num_total_ly,
  111. z1.zps_num_total as zps_num_total_cm,
  112. avg_num(z1.zps_num_total,z2.zps_num_total) as zps_num_with5g,
  113. dgc1.grid_code
  114. from
  115. (select * from sqmdb_cost.zpzs where smonth='202204') z1
  116. join
  117. (select * from sqmdb_cost.zpzs where smonth='202112') z2
  118. on trim(z1.city_name)=trim(z2.city_name) and trim(z1.grid_name)=trim(z2.grid_name)
  119. left join sqmdb_cost.dict_grid_code dgc1 on trim(z1.city_name)=trim(dgc1.city_name) and trim(z1.grid_name)=trim(dgc1.grid_name)
  120. where dgc1.grid_code is not null) fm
  121. on fz.grid_code=fm.grid_code
  122. left join sqmdb_cost.dict_code_grid dcg on fz.grid_code=dcg.grid_code
  123. --网格端口数:天粒度城区、县城、农村按区域汇总,当月天平均值
  124. --create table sqmdb_cost.kuandai_ftth_month as
  125. select
  126. '202204' as smonth,
  127. city_name_short(local_net_name) as city_name,area_name,
  128. avg(ftth_port_count) as ftth_port_count,
  129. avg(ftth_port_sts_count) as ftth_port_sts_count
  130. from
  131. (select
  132. sdate,local_net_name,area_name,
  133. sum(ftth_port_count) as ftth_port_count,
  134. sum(ftth_port_sts_count) as ftth_port_sts_count
  135. from sqmdb_cost.kuandai_ftth kf
  136. where areal_type_name!='小计'
  137. and sdate>=date_trunc('month',to_date('202204','yyyymm'))
  138. and sdate<date_trunc('month',to_date('202204','yyyymm') + interval '1' month)
  139. group by sdate,local_net_name,area_name) day_area_sum
  140. group by local_net_name,area_name
  141. --单端口能耗、网运成本--网格
  142. --核查网格单端口能耗和地市单端口能耗差异问题(t1表去掉where条件作为分子,t2表作为分母,分别汇总算地市值,单独计算网格值)
  143. --drop table sqmdb_cost.rpt_dport_cost_grid_1
  144. --create table sqmdb_cost.rpt_dport_cost_grid_1 as
  145. select t1.smonth,dcg.city_name,dcg.grid_name,t1.cost_total,t1.cost_nenghao,t2.ftth_port_sts_count,
  146. t1.cost_total/t2.ftth_port_sts_count/4 as cost_port,
  147. t1.cost_nenghao/t2.ftth_port_sts_count/4 as cost_port_nenghao,
  148. dcg.city_code,dcg.grid_code
  149. from
  150. (select jt.smonth,jt.city_name,jt.grid_name,jt.cost_total,jt.cost_nenghao,dgc.grid_code from
  151. (select smonth,period_type,segment1_name,city_code,city_name_short(city_name) as city_name,grid_code,grid_name,
  152. (column1-column2-column3-column4-column5-column6-column7-column8-column9-column10-column11-column12-column13-column14-column15-column16-column17-column18-column19-column20-column21-column22-column23-column24-column25-column26-column108-column109-column110-column111-column112-column113-column114)/10000 as cost_total,
  153. (column5+column19+column27+column41+column49+column57+column70+column78+column86+column94+column100+column108+column115-column5-column19-column108)/10000 as cost_nenghao
  154. from sqmdb_cost.report49
  155. where period_type='Y' and grid_name!='合计' and smonth='202204') jt
  156. left join sqmdb_cost.dict_grid_code dgc on jt.city_name=dgc.city_name and trim(jt.grid_name)=trim(dgc.grid_name)
  157. where dgc.grid_code is not null) t1
  158. join
  159. (select a.city_name,a.area_name,a.ftth_port_sts_count,dgc1.grid_code from
  160. (select sdate,city_name_short(local_net_name) as city_name,area_name,sum(ftth_port_sts_count)/10000 as ftth_port_sts_count
  161. from sqmdb_cost.kuandai_ftth
  162. where areal_type_name!='小计' and sdate='2022-04-30 00:00:00'
  163. group by sdate,city_name_short(local_net_name),area_name) a
  164. left join sqmdb_cost.dict_grid_code dgc1 on a.city_name=dgc1.city_name and trim(a.area_name)=trim(dgc1.grid_name)) t2
  165. on t1.grid_code=t2.grid_code
  166. left join sqmdb_cost.dict_code_grid dcg on t1.grid_code=dcg.grid_code
  167. select t1.smonth,dcg.city_name,dcg.grid_name,t1.cost_total,t1.cost_nenghao,t2.ftth_port_sts_count,
  168. t1.cost_total/t2.ftth_port_sts_count/4 as cost_port,
  169. t1.cost_nenghao/t2.ftth_port_sts_count/4 as cost_port_nenghao,
  170. dcg.city_code,dcg.grid_code
  171. from
  172. (select jt.smonth,jt.city_name,jt.grid_name,jt.cost_total,jt.cost_nenghao,dgc.grid_code from
  173. (select smonth,period_type,segment1_name,city_code,city_name_short(city_name) as city_name,grid_code,grid_name,
  174. (column1-column2-column3-column4-column5-column6-column7-column8-column9-column10-column11-column12-column13-column14-column15-column16-column17-column18-column19-column20-column21-column22-column23-column24-column25-column26-column108-column109-column110-column111-column112-column113-column114)/10000 as cost_total,
  175. (column5+column19+column27+column41+column49+column57+column70+column78+column86+column94+column100+column108+column115-column5-column19-column108)/10000 as cost_nenghao
  176. from sqmdb_cost.report49
  177. where period_type='Y' and grid_name!='合计' and smonth='202204') jt
  178. left join sqmdb_cost.dict_grid_code dgc on jt.city_name=dgc.city_name and trim(jt.grid_name)=trim(dgc.grid_name)
  179. where dgc.grid_code is not null) t1
  180. join
  181. (select a.city_name,a.area_name,a.ftth_port_sts_count,dgc1.grid_code from
  182. (select p2.smonth,p2.city_name,p2.area_name,
  183. avg_num(p1.ftth_port_sts_count,p2.ftth_port_sts_count)/10000 as ftth_port_sts_count from
  184. (select * from sqmdb_cost.kuandai_ftth_month where smonth='202112')p1
  185. join
  186. (select * from sqmdb_cost.kuandai_ftth_month where smonth='202204')p2
  187. on p1.city_name=p2.city_name and p1.area_name=p2.area_name) a
  188. left join sqmdb_cost.dict_grid_code dgc1 on a.city_name=dgc1.city_name and trim(a.area_name)=trim(dgc1.grid_name)) t2
  189. on t1.grid_code=t2.grid_code
  190. left join sqmdb_cost.dict_code_grid dcg on t1.grid_code=dcg.grid_code
  191. --以下是6个报表
  192. --1 单载频载扇能耗
  193. ---网格报表
  194. --drop table sqmdb_cost.rpt_baobiao_1_dzpz
  195. --create table sqmdb_cost.rpt_baobiao_1_dzpz as
  196. select
  197. grid.city_name,
  198. grid.grid_name,
  199. grid.cost_zps_with5g as cost_grid,
  200. city.cost_zps_with5g as cost_city,
  201. grid.cost_zps_with5g/city.cost_zps_with5g as cost_over_city,
  202. row_number() over(partition by grid.smonth,grid.city_name order by grid.cost_zps_with5g) as rn_city,
  203. prov.cost_zps_with5g as cost_prov,
  204. grid.cost_zps_with5g/prov.cost_zps_with5g as cost_over_prov,
  205. row_number() over(partition by grid.smonth order by grid.cost_zps_with5g) as rn_prov,
  206. grid.city_code*100 as city_code,
  207. grid.grid_code,
  208. grid.smonth,
  209. dcg.sort_id as sort_id_city
  210. from
  211. (select * from sqmdb_cost.rpt_dzpzs_cost_grid) grid
  212. left join
  213. (select smonth,city_sname,cost_zps_with5g from sqmdb_cost.rpt_dzpzs_cost_city) city
  214. on grid.smonth=city.smonth and grid.city_name=city.city_sname
  215. left join
  216. (select smonth,cost_zps_with5g from sqmdb_cost.rpt_dzpzs_cost_prov) prov
  217. on grid.smonth=prov.smonth
  218. join sqmdb_cost.dict_code_grid dcg on grid.grid_code=dcg.grid_code
  219. where grid.cost_zps_with5g>0
  220. and grid.smonth='202205'
  221. order by dcg.sort_id,rn_city
  222. --2 单端口能耗
  223. --drop table sqmdb_cost.rpt_baobiao_2_ddk
  224. --create table sqmdb_cost.rpt_baobiao_2_ddk as
  225. select
  226. grid.city_name,
  227. grid.grid_name,
  228. grid.cost_port_nenghao,
  229. city.cost_port_nenghao_city,
  230. grid.cost_port_nenghao/city.cost_port_nenghao_city as over_cost_city,
  231. row_number() over(partition by grid.smonth,grid.city_name order by grid.cost_port_nenghao) as rn_city,
  232. prov.cost_port_nenghao_prov,
  233. grid.cost_port_nenghao/prov.cost_port_nenghao_prov as over_cost_prov,
  234. row_number() over(partition by grid.smonth order by grid.cost_port_nenghao) as rn_prov,
  235. grid.city_code*100 as city_code,
  236. grid.grid_code,
  237. grid.smonth,
  238. dcg.sort_id as sort_id_city
  239. from
  240. (select * from sqmdb_cost.rpt_dport_cost_grid) grid
  241. left join
  242. (select smonth,city_sname,cost_port_nenghao as cost_port_nenghao_city from sqmdb_cost.rpt_dport_cost_city) city
  243. on grid.smonth=city.smonth and grid.city_name=city.city_sname
  244. left join
  245. (select smonth,cost_port_nenghao as cost_port_nenghao_prov from sqmdb_cost.rpt_dport_cost_b10 where sname='河北') prov
  246. on grid.smonth=prov.smonth
  247. join sqmdb_cost.dict_code_grid dcg on grid.grid_code=dcg.grid_code
  248. where grid.smonth='202205'
  249. order by dcg.sort_id,rn_city
  250. --3 单站租赁费
  251. --drop table sqmdb_cost.rpt_baobiao_3_dzzl
  252. --create table sqmdb_cost.rpt_baobiao_3_dzzl as
  253. select
  254. tw.city_name,
  255. --tw.area_operator,
  256. tw.grid_name,
  257. round(tw.tower_cost_avg_area) as tower_cost_avg_area,
  258. round(tw.tower_cost_avg_city) as tower_cost_avg_city,
  259. round(tw.over_cost_city,2) as tower_over_cost_city,
  260. tw.rn_city as tower_rn_city,
  261. round(tw.tower_cost_avg_prov) as tower_cost_avg_prov,
  262. round(tw.over_cost_prov,2) as tower_over_cost_prov,
  263. tw.rn_prov as tower_rn_prov,
  264. round(ht.avg_cost_grid) as ht_avg_cost_grid,
  265. round(ht.avg_cost_city) as ht_avg_cost_city,
  266. round(ht.over_cost_city,2) as ht_over_cost_city,
  267. ht.rn_city as ht_rn_city,
  268. round(ht.avg_cost_prov) as ht_avg_cost_prov,
  269. round(ht.over_cost_prov,2) as ht_over_cost_prov,
  270. ht.rn_prov as ht_rn_prov,
  271. tw.city_code,
  272. tw.grid_code,
  273. tw.smonth,
  274. dcg.sort_id as sort_id_city
  275. from
  276. (select
  277. tg.smonth,
  278. tg.city_name,
  279. tg.grid_name,
  280. tg.tower_cost_avg as tower_cost_avg_area,
  281. tc.tower_cost_avg as tower_cost_avg_city,
  282. tg.tower_cost_avg/tc.tower_cost_avg as over_cost_city,
  283. row_number() over(partition by tg.smonth,tg.city_name order by tg.tower_cost_avg) as rn_city,
  284. tp.tower_cost_avg_prov,
  285. tg.tower_cost_avg/tp.tower_cost_avg_prov as over_cost_prov,
  286. row_number() over(partition by tg.smonth order by tg.tower_cost_avg) as rn_prov,
  287. tg.city_code*100 as city_code,
  288. tg.grid_code
  289. from sqmdb_cost.rpt_tower_cost_grid tg
  290. left join sqmdb_cost.rpt_tower_cost_city tc on tg.smonth=tc.smonth and tg.city_name=tc.city_name
  291. left join
  292. (select smonth,sname,column1 as tower_cost_avg_prov from sqmdb_cost.report50016 where smonth='202206' and sname='河北') tp
  293. on tg.smonth=tp.smonth
  294. where tg.smonth='202206'
  295. order by tg.city_code,rn_city) tw
  296. left join
  297. (with station_cost as ( --合同
  298. select city_name_short(s.city_name) as city_name,s.area_name,s.station_sid,s.station_sname,h.sid,h.sname,h.yuezujin
  299. from sqmdb_cost.station3 s
  300. join sqmdb_cost.station_hetong sh on s.station_sid=sh.station_sid
  301. join sqmdb_cost.hetong_new h on h.sid=sh.hetong_sid
  302. where h.is_tc='否' and h.yuezujin is not null)
  303. select
  304. grid.smonth,
  305. grid.city_name,
  306. grid.area_name,
  307. grid.avg_cost_grid,
  308. city.avg_cost_city,
  309. grid.avg_cost_grid/city.avg_cost_city as over_cost_city,
  310. row_number() over(partition by grid.smonth,grid.city_name order by grid.avg_cost_grid) as rn_city,
  311. prov.avg_cost_prov,
  312. grid.avg_cost_grid/prov.avg_cost_prov as over_cost_prov,
  313. row_number() over(partition by grid.smonth order by grid.avg_cost_grid) as rn_prov,
  314. dgc.grid_code
  315. from
  316. (select '202206' as smonth,city_name,area_name,avg(yuezujin) as avg_cost_grid from station_cost group by city_name,area_name) grid
  317. left join
  318. (select '202206' as smonth,city_name,avg(yuezujin) as avg_cost_city from station_cost group by city_name) city
  319. on grid.city_name=city.city_name
  320. left join
  321. (select '202206' as smonth,avg(yuezujin) as avg_cost_prov from station_cost) prov
  322. on grid.smonth=prov.smonth
  323. left join sqmdb_cost.dict_grid_code dgc on grid.city_name=dgc.city_name and grid.area_name=dgc.grid_name) ht
  324. on tw.grid_code=ht.grid_code
  325. join sqmdb_cost.dict_code_grid dcg on tw.grid_code=dcg.grid_code
  326. order by dcg.sort_id,tw.rn_city
  327. --4 超高租费
  328. --drop table sqmdb_cost.rpt_baobiao_4_cgzf
  329. --create table sqmdb_cost.rpt_baobiao_4_cgzf as
  330. select distinct
  331. dg.city_sname, --地市分公司
  332. --t1.city_name,
  333. t1.station_sname,--局站名称
  334. t1.area_name, --区县名称
  335. t1.station_sid, --局站ID
  336. t1.station_type,--局站类型
  337. t1.station_level,--所属场景
  338. t1.property_type,--权属类别
  339. t1.tower_cost, --铁塔租金
  340. t1.changd_cost, --其中:场租
  341. t1.zhejiu_cost, --其中:折旧
  342. t1.service_property,--业务属性
  343. t1.production_type, --产品类型
  344. t1.site_addr_id, --铁塔站址编码
  345. t1.tower_cbzx, --成本中心
  346. tchang.changd_cost_grid, --场租(网格)
  347. t1.changd_cost/tchang.changd_cost_grid as over_changd_cost_grid,--场租倍数(网格)
  348. tchang.tower_cost_grid, --租赁费 本网格
  349. case when tchang.tower_cost_grid=0 then null else t1.tower_cost/tchang.tower_cost_grid end as over_tower_cost_grid,--租赁费倍数(网格)
  350. tchang.changd_cost_city, --场租(地市)
  351. t1.changd_cost/tchang.changd_cost_city as over_changd_cost_city,--场租倍数(地市)
  352. tchang.tower_cost_city, --租赁费 本地市
  353. t1.tower_cost/tchang.tower_cost_city as over_tower_cost_city,--租赁费倍数(地市)
  354. min(t1.changd_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) as changd_cost_grid_scene_good, --场租:本场景本网格最优
  355. avg(t1.changd_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) as changd_cost_grid_scene_avg, --场租:本场景本网格均值
  356. case when avg(t1.changd_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type)=0 then null else
  357. t1.changd_cost/avg(t1.changd_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) end as changd_cost_grid_scene_bs, --场租:场租倍数
  358. min(t1.changd_cost)over(partition by t1.city_name,t1.station_level,t1.service_property,t1.production_type) as changd_cost_city_scene_good, --场租:本场景本地市网格内最优
  359. avg(t1.changd_cost)over(partition by t1.city_name,t1.station_level,t1.service_property,t1.production_type) as changd_cost_city_scene_avg, --场租:本场景本地市网格内均值
  360. min(t1.tower_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) as tower_cost_grid_scene_good, --铁塔租赁费:本场景本网格最优
  361. avg(t1.tower_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) as tower_cost_grid_scene_avg, --铁塔租赁费:本场景本网格均值
  362. case when avg(t1.tower_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type)=0 then null else
  363. t1.tower_cost/avg(t1.tower_cost)over(partition by t1.city_name,t1.area_name,t1.station_level,t1.service_property,t1.production_type) end as tower_cost_grid_scene_bs, --铁塔租赁费:租赁费倍数
  364. min(t1.tower_cost)over(partition by t1.city_name,t1.station_level,t1.service_property,t1.production_type) as tower_cost_city_scene_good, --铁塔租赁费:本场景本地市网格内最优
  365. avg(t1.tower_cost)over(partition by t1.city_name,t1.station_level,t1.service_property,t1.production_type) as tower_cost_city_scene_avg, --铁塔租赁费:本场景本地市网格内均值
  366. t1.hetong_yuezujin as cost_hetong, --合同租金
  367. t1.sid as hetong_sid, --合同编号
  368. t1.hetong_cbzx, --合同成本中心
  369. min(t1.hetong_yuezujin)over(partition by t1.city_name,t1.area_name,t1.station_level) as ht_cost_grid_scene_good,--本场景本网格最优
  370. min(t1.hetong_yuezujin)over(partition by t1.city_name,t1.station_level) as ht_cost_city_scene_good, --本场景本地市网格内最优
  371. avg(t1.hetong_yuezujin)over(partition by t1.city_name,t1.area_name,t1.station_level) as ht_cost_grid_scene_avg, --本场景本地市网格内均值
  372. case when avg(t1.hetong_yuezujin)over(partition by t1.city_name,t1.area_name,t1.station_level)=0 then null else
  373. t1.hetong_yuezujin/avg(t1.hetong_yuezujin)over(partition by t1.city_name,t1.area_name,t1.station_level) end as ht_cost_grid_scene_bs, --租金倍数
  374. min(t1.hetong_yuezujin)over(partition by t1.city_name,t1.station_level,t1.hetong_cbzx) as ht_cost_cbzx_good, --本场景本单位最优
  375. dg.city_code*100 as city_code,
  376. '202206' as smonth,
  377. dgc.grid_code,
  378. dg.sort_id as sort_id_city,
  379. (case t1.station_level
  380. when '城市' then 1
  381. when '县城' then 2
  382. when '乡镇' then 3
  383. when '农村' then 4
  384. else 5 end) as station_level_id
  385. from (
  386. select
  387. a.station_sid,
  388. a.station_sname,
  389. a.city_name,
  390. a.area_name,
  391. a.station_type,
  392. a.property_type,
  393. a.station_level,
  394. a.tower_code,
  395. c.yuezujin as hetong_yuezujin,--合同系统月租金(由合同总月份数和总金额计算得出)
  396. c.cname,
  397. c.cfname,
  398. c.cbzx as hetong_cbzx,
  399. c.sid,
  400. c.sname,
  401. d.sdate,
  402. e.cbzx as tower_cbzx,
  403. d.site_addr_id,
  404. d.total_cost as tower_cost,
  405. d.changd_cost,
  406. d.zhejiu_cost,
  407. d.service_property,
  408. d.production_type,
  409. (coalesce(c.yuezujin,0)+coalesce(d.total_cost,0)) as cost_total,--总费用(合同月租金使用合同系统值)
  410. case when c.cbzx is null then e.cbzx else c.cbzx end as cbzx
  411. from sqmdb_cost.station3 a
  412. left join sqmdb_cost.station_hetong b on a.station_sid=b.station_sid
  413. left join (select * from sqmdb_cost.hetong_new where is_tc='否') c on c.sid=b.hetong_sid
  414. left join
  415. (select sdate,site_addr_id,service_property,production_type,
  416. sum(total_cost) as total_cost,
  417. sum(changd_cost) as changd_cost,
  418. sum(zhejiu_cost) as zhejiu_cost
  419. from sqmdb_cost.tower3
  420. where sdate='202206'
  421. group by sdate,site_addr_id,service_property,production_type) d
  422. on a.tower_code=d.site_addr_id
  423. left join sqmdb_cost.dict_tower_cbzx e on a.tower_code=e.tower_code) t1
  424. left join
  425. (select * from sqmdb_cost.v_tower_cost_changd where sdate='202206') tchang
  426. on t1.city_name=tchang.city_operator and t1.area_name=tchang.area_name
  427. left join sqmdb_cost.dict_grid_code dgc on city_name_short(t1.city_name)=dgc.city_name and trim(t1.area_name)=trim(dgc.grid_name)
  428. join
  429. (select distinct city_code,city_name,city_sname,sort_id from sqmdb_cost.dict_grid) dg
  430. on t1.city_name=dg.city_name
  431. where t1.site_addr_id is not null or t1.sid is not null
  432. order by sort_id_city,
  433. (case t1.station_level
  434. when '城市' then 1
  435. when '县城' then 2
  436. when '乡镇' then 3
  437. when '农村' then 4
  438. else 5 end),
  439. t1.service_property,t1.production_type,t1.changd_cost desc
  440. --5 转供电超高电费单价站点
  441. --drop table sqmdb_cost.rpt_baobiao_5_cgdf
  442. --create table sqmdb_cost.rpt_baobiao_5_cgdf as
  443. select distinct
  444. dg.city_sname,
  445. s.station_sid,
  446. s.station_sname,
  447. yn.build_name,
  448. s.station_type,
  449. s.station_level,
  450. s.property_type,
  451. case when yn.accrued_power=0 then 0 else yn.accrued_cost/yn.accrued_power end as eprice,
  452. yn.cost_unit_name,
  453. dg.city_code*100 as city_code,
  454. yn.smonth,
  455. dgc.grid_code,
  456. yn.area_name,
  457. dg.sort_id as sort_id_city
  458. from sqmdb_cost.ytmx_new yn
  459. left join sqmdb_cost.station_nenghao sn on yn.build_code_short=sn.oss_station
  460. left join sqmdb_cost.station3 s on sn.station_sid=s.station_sid
  461. left join sqmdb_cost.dict_grid_code dgc on city_name_short(yn.city_name)=dgc.city_name and trim(yn.area_name)=trim(dgc.grid_name)
  462. left join (select distinct city_name,city_code,city_sname,sort_id from sqmdb_cost.dict_grid) dg on yn.city_name=dg.city_name
  463. where yn.meter_type_name in ('12-01 | 转供手抄表')
  464. and yn.build_type_name not in ('办公楼','办公用房','营业用房')
  465. and yn.smonth='202207'
  466. order by dg.sort_id,s.station_level
  467. --6 超高能耗站点
  468. --drop table sqmdb_cost.rpt_baobiao_6_cgnh
  469. --create table sqmdb_cost.rpt_baobiao_6_cgnh as
  470. select distinct
  471. dg.city_sname,
  472. s.station_sname,
  473. s.area_name,
  474. s.station_type,
  475. s.station_level,
  476. s.property_type,
  477. yn.meter_type_name,
  478. yn.accrued_power,
  479. case when yn.accrued_power=0 then 0 else yn.accrued_cost/yn.accrued_power end as eprice,
  480. yn.accrued_cost,
  481. yn.cost_unit_name,
  482. wg_nh.nh_total as lilun_power,
  483. case when wg_nh.nh_total=0 then 0 else yn.accrued_power/wg_nh.nh_total end as over_lilun_power,
  484. cec.city_eprice as lilun_eprice,
  485. cec.city_eprice*wg_nh.nh_total as lilun_cost,
  486. case when wg_nh.nh_total=0 then 0 else yn.accrued_cost/(cec.city_eprice*wg_nh.nh_total) end as over_lilun_cost,
  487. dg.city_code*100 as city_code,
  488. yn.smonth,
  489. dgc.grid_code,
  490. yn.build_code_short as nh_sid_short,
  491. s.station_sid,
  492. dg.sort_id as sort_id_city
  493. from sqmdb_cost.ytmx_new yn
  494. left join sqmdb_cost.station_nenghao sn on yn.build_code_short=sn.oss_station
  495. left join sqmdb_cost.station3 s on sn.station_sid=s.station_sid
  496. left join sqmdb_cost.dict_grid_code dgc on city_name_short(yn.city_name)=dgc.city_name and trim(yn.area_name)=trim(dgc.grid_name)
  497. left join (select distinct city_name,city_code,city_sname,sort_id from sqmdb_cost.dict_grid) dg on yn.city_name=dg.city_name
  498. left join sqmdb_cost.cfg_eprice_city cec on dg.city_sname=cec.city_name
  499. join sqmdb_cost.v_station_omc_nh_month wg_nh --专业网管能耗
  500. on yn.smonth=wg_nh.smonth and sn.station_sid=wg_nh.station_sid
  501. where yn.smonth='202207'
  502. order by dg.sort_id
  503. --有合同无局站
  504. select
  505. distinct
  506. '202207' as smonth,
  507. h.city_name,
  508. h.cname,
  509. h.dname,
  510. h.sid,
  511. h.sname,
  512. h.cbzx,
  513. sh.station_sid as idx_station_sid,
  514. sh.hetong_sid as idx_hetong_sid,
  515. (case when sh.hetong_sid is null then '有合同无局站' else '有合同有局站' end) as stype,
  516. dgc.grid_code,
  517. dc.sort_id as sort_id_city
  518. from
  519. (select city_name,sid,sname,cname,dname,cbzx from sqmdb_cost.hetong_new where is_tc='否') h
  520. left join sqmdb_cost.station_hetong sh
  521. on h.sid=sh.hetong_sid
  522. left join sqmdb_cost.dict_grid_code dgc on h.city_name=dgc.city_name and trim(h.dname)=trim(dgc.grid_name)
  523. left join sqmdb_cost.dict_city dc on h.city_name=dc.city_name
  524. order by dc.sort_id
  525. --有能耗无局站
  526. select
  527. distinct
  528. yn.smonth,
  529. yn.build_code_short,
  530. yn.city_name,
  531. yn.area_name,
  532. yn.build_name,
  533. yn.cost_unit_name,
  534. yn.accrued_power,
  535. yn.accrued_cost,
  536. s.city_name as idx_city_name,
  537. sn.oss_station as idx_nh_short_id,
  538. sn.station_sid as idx_station_sid,
  539. s.station_sname as idx_station_name,
  540. wg.station_sid as omc_station_sid,
  541. wg.nh_total as omc_nh_total,
  542. (case
  543. when s.oss_station is not null and yn.accrued_cost!=0 and yn.accrued_cost is not null and wg.station_sid is not null then '校验正常'
  544. when s.oss_station is null then '有能耗无索引'
  545. when yn.accrued_cost is null or yn.accrued_cost=0 then '有能耗无成本'
  546. when yn.accrued_cost!=0 and wg.station_sid is null then '有能耗有成本,校验有问题'
  547. end) as stype,
  548. dgc.grid_code,
  549. dc.sort_id as sort_id_city
  550. from
  551. (select * from sqmdb_cost.ytmx_new where build_code_short is not null and smonth='202207') yn
  552. left join sqmdb_cost.station_nenghao sn on yn.build_code_short=sn.oss_station
  553. left join sqmdb_cost.station3 s on sn.station_sid=s.station_sid
  554. left join
  555. (select * from sqmdb_cost.v_station_omc_nh_month where smonth='202207') wg
  556. on sn.station_sid=wg.station_sid
  557. left join sqmdb_cost.dict_grid_code dgc on city_name_short(yn.city_name)=dgc.city_name and trim(yn.area_name)=trim(dgc.grid_name)
  558. left join sqmdb_cost.dict_city dc on yn.city_name=dc.city_name
  559. order by dc.sort_id
  560. --有铁塔订单无局站
  561. select
  562. distinct
  563. t.sdate as smonth,
  564. t.city_operator,
  565. t.area_operator,
  566. t.site_addr_id,
  567. dtc.cbzx,
  568. s.station_sid,
  569. s.station_sname,
  570. s.property_type,
  571. s.station_level,
  572. case when s.tower_code is null then '有铁塔订单无局站' else '有铁塔订单有局站' end as stype,
  573. dgc.grid_code,
  574. dc.sort_id as sort_id_city
  575. from
  576. (select distinct sdate,city_operator,area_operator,site_addr_id,site_name
  577. from sqmdb_cost.tower3 where sdate='202207') t
  578. left join
  579. (select station_sid,station_sname,property_type,station_level,tower_code from sqmdb_cost.station3) s
  580. on t.site_addr_id=s.tower_code
  581. left join sqmdb_cost.dict_grid_code dgc on city_name_short(t.city_operator)=dgc.city_name and trim(t.area_operator)=trim(dgc.grid_name)
  582. left join sqmdb_cost.dict_tower_cbzx dtc on t.site_addr_id=dtc.tower_code
  583. left join sqmdb_cost.dict_city dc on t.city_operator=dc.city_name
  584. order by dc.sort_id