成本大屏租赁费中间表.sql 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. --drop table sqmdb_cost.rpt_3rd_tower_cost
  2. --create table sqmdb_cost.rpt_3rd_tower_cost as
  3. select
  4. tgrid.smonth,
  5. a.*,
  6. tgrid.tower_cost_avg,
  7. dc.sort_id as sort_id_city
  8. from
  9. (select
  10. distinct
  11. s.station_sid,
  12. s.station_sname,
  13. s.city_name,
  14. s.area_name,
  15. s.property_type,
  16. s.station_level,
  17. s.tower_code,
  18. hn.sid as ht_sid,
  19. hn.sname as ht_sname,
  20. hn.yuezujin as ht_yuezujin,
  21. hn.cbzx as ht_cbzx,
  22. t.site_addr_id,
  23. t.total_cost as tower_cost,
  24. dtc.tower_name,
  25. dtc.cbzx as tower_cbzx,
  26. (coalesce(hn.yuezujin,0)+coalesce(t.total_cost,0)) as cost_total,
  27. case
  28. when hn.sid is not null and t.site_addr_id is not null then '租用铁塔+租用第三方'
  29. when hn.sid is not null and t.site_addr_id is null then '租用第三方'
  30. when hn.sid is null and t.site_addr_id is not null then '租用铁塔'
  31. else '' end as zulin_type,
  32. dgc.grid_code
  33. from sqmdb_cost.station3 s
  34. left join sqmdb_cost.station_hetong sh on s.station_sid=sh.station_sid
  35. left join sqmdb_cost.hetong_new hn on sh.hetong_sid=hn.sid
  36. left join
  37. (select site_addr_id,sum(total_cost) as total_cost from sqmdb_cost.tower3 where sdate='202205' group by site_addr_id) t
  38. on s.tower_code=t.site_addr_id
  39. left join sqmdb_cost.dict_tower_cbzx dtc on t.site_addr_id=dtc.tower_code
  40. left join sqmdb_cost.dict_grid_code dgc on city_name_short(s.city_name)=dgc.city_name and trim(s.area_name)=dgc.grid_name
  41. where s.station_sid>0 and (hn.sid is not null or t.total_cost is not null))a
  42. join
  43. (select smonth,grid_code,tower_cost_avg from sqmdb_cost.rpt_tower_cost_grid where smonth='202205') tgrid
  44. on a.grid_code=tgrid.grid_code
  45. left join sqmdb_cost.dict_city dc on a.city_name=dc.city_name
  46. order by dc.sort_id