chengben_exception_1_grid.sql 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. --租赁费(chengben_exception_1_grid_202203.csv)
  2. ----横向匹配 局站表a <- 合同索引b <- 合同系统c <- 铁塔全量表d
  3. select tb1.*,tb2.city_code*100 as city_code,tb3.area_code from
  4. (select
  5. distinct
  6. t1.sdate,
  7. t1.city_name,
  8. t1.area_name,
  9. t1.station_sid,
  10. t1.station_sname,
  11. t1.property_type,
  12. t1.sid as hetong_sid,
  13. t1.hetong_yuezujin as cost_hetong,
  14. t1.tower_code,
  15. t1.tower_cost as cost_tower,
  16. t1.cost_total,
  17. t2.cbzx,
  18. t2.cbzx_cost_avg,
  19. t1.cost_total/t2.cbzx_cost_avg as cost_rate
  20. from (
  21. select
  22. a.station_sid,a.station_sname,a.city_name,a.area_name,a.property_type,a.station_level,a.tower_code,
  23. c.yuezujin as hetong_yuezujin,--合同系统月租金(由合同总月份数和总金额计算得出)
  24. c.cname,c.cfname,c.cbzx as hetong_cbzx,c.sid,c.sname,
  25. d.sdate,e.cbzx as tower_cbzx,d.site_addr_id,d.total_cost as tower_cost,
  26. (coalesce(c.yuezujin,0)+coalesce(d.total_cost,0)) as cost_total,--总费用(合同月租金使用合同系统值)
  27. case when c.cbzx is null then e.cbzx else c.cbzx end as cbzx
  28. from sqmdb_cost.station3 a
  29. left join sqmdb_cost.station_hetong b on a.station_sid=b.station_sid
  30. left join sqmdb_cost.hetong_new c on c.sid=b.hetong_sid
  31. left join (select sdate,site_addr_id,sum(total_cost) as total_cost from sqmdb_cost.tower3 where sdate='202204' group by sdate,site_addr_id) d on a.tower_code=d.site_addr_id
  32. left join sqmdb_cost.dict_tower_cbzx e on a.tower_code=e.tower_code) t1
  33. left join (select * from sqmdb_cost.v_tower_cost_cbzx where sdate='202204') t2
  34. on t1.sdate=t2.sdate::varchar and t1.cbzx=t2.cbzx
  35. where t1.cost_total/t2.cbzx_cost_avg>1.5) tb1
  36. left join
  37. (select distinct city_code,city_name from sqmdb_cost.dict_grid) tb2
  38. on tb1.city_name=tb2.city_name
  39. left join
  40. (select distinct city_code,city_name,area_code,area_name from sqmdb_cost.dict_grid) tb3
  41. on tb1.city_name=tb3.city_name and tb1.area_name=tb3.area_name
  42. order by tb2.city_code,tb3.area_code