sql子查询聚合

时间:2019-01-20 02:49:35

标签: sql amazon-redshift nested-select

我有一个相当复杂的多重嵌套查询集,需要将其聚合到一个视图中,但是我一直在获取度量标准不存在错误。每个组件运行正常,但是当我将它们全部放在一起时,会出现错误并指出无效的操作:函数sum(数字,数字,数字)不存在

任何帮助将不胜感激

 with LM_DSP as 
    (Select 
      metrics.reportingdate
      --,to_char(dateadd(DAY,1,reportingdate), 'IYYY-IW') as reporting_week
      ,metrics.location_allocated
      ,metrics.country_code
      ,nvl(sm.sub_region,'undesignated') as director
      ,nvl(sm.area,'undesignated') as region
      ,metrics.route_type
      ,sum(case when metrics_id = 1 then value else 0 end) as Delivered_Shipments
      ,sum(case when metrics_id =5 then value else 0 end) as LM_hours
      ,sum(case when metrics_id = 6 then value else 0 end) as LM_routes --DSP and _DA routes only
      ,sum(case when metrics_id in ('8','9','10','11','46','12','13','14','15','16','17','18','43','44','45') then value else 0 end) as LM_cost
      ,sum(case when metrics_id = 8 then value else 0 end ) as LM_base_cost_dsp
      ,sum(case when metrics_id = 9 then value else 0 end ) as LM_branding_cost_dsp
      ,sum(case when metrics_id = 10 then value else 0 end ) as LM_parking_cost_dsp
      ,sum(case when metrics_id = 11 then value else 0 end ) as LM_fuel_cost_dsp
      ,sum(case when metrics_id = 46 then value else 0 end ) as LM_fuel_card_cost_dsp
      ,sum(case when metrics_id = 12 then value else 0 end ) as LM_dispatcher_cost_dsp
      ,sum(case when metrics_id = 13 then value else 0 end ) as LM_tech_cost_dsp
      ,sum(case when metrics_id = 14 then value else 0 end ) as LM_deprecation_cost_dsp
      ,sum(case when metrics_id = 15 then value else 0 end ) as LM_undesignated_cost_dsp
      ,sum(case when metrics_id = 16 then value else 0 end ) as LM_inperiod_offmanifest_cost_dsp
      ,sum(case when metrics_id = 17 then value else 0 end ) as LM_outperiod_offmanifest_cost_dsp
      ,sum(case when metrics_id = 18 then value else 0 end ) as LM_outperiod_manifest_cost_dsp
      ,sum(case when metrics_id = 43 then value else 0 end ) as LM_monthly_fixed_fee_cost_dsp
      ,sum(case when metrics_id = 44 then value else 0 end ) as LM_variable_per_piece_cost_dsp
      ,sum(case when metrics_id = 45 then value else 0 end ) as LM_variable_branding_per_piece_cost_dsp
    from 
      _finance.master_metrics_v2 metrics left join _bi_ddl.o_comp_stations_master sm on
      metrics.location_allocated = sm.station_code
      and SM.STATUS IN ('A', 'H')
      and sm.country_code in ('US','CA')
      and provider_type not in ('_DA')
    where
       --reportingdate >= '2017-12-31 00:00:00'
      reportingdate>='2019-01-06'
      and reportingdate <='2019-01-13'
      and provider_type = 'DSP'
    group by
      reportingdate
      ,metrics.location_allocated
      ,metrics.country_code
      ,provider_type
      ,metrics.route_type
      ,director
       ,region
    ),
    LM_ADA as 
    (Select 
      metrics.reportingdate
      --,to_char(dateadd(DAY,1,reportingdate), 'IYYY-IW') as reporting_week 
      ,metrics.location_allocated
      ,metrics.country_code
      ,nvl(sm.sub_region,'undesignated') as director
      ,nvl(sm.area,'undesignated') as region
      ,metrics.route_type
      ,sum(case when metrics_id = 1 then value else 0 end) as Delivered_Shipments
      ,sum(case when metrics_id =48 then value else 0 end) as LM_hours
      ,sum(case when metrics_id = 49 then value else 0 end) as LM_routes
      ,sum(case when metrics_id in ('8','9','10','11','46','12','13','14','15','16','17','18','43','44','45') then value else 0 end) as LM_cost
    from 
      _finance.master_metrics_v2 metrics left join _bi_ddl.o_comp_stations_master sm on
      metrics.location_allocated = sm.station_code
      and SM.STATUS IN ('A', 'H')
      and sm.country_code in ('US','CA')
      and provider_type = '_DA'
    where
      --reportingdate >= '2017-12-31 00:00:00'
      reportingdate>='2019-01-06'
      and reportingdate <='2019-01-13'
    group by
      reportingdate
      ,metrics.location_allocated
      ,metrics.country_code
      ,provider_type
      ,metrics.route_type
      ,director
      ,region
    ),
    LM_f as 
    (Select 
      metrics.reportingdate
      --to_char(dateadd(DAY,1,reportingdate), 'IYYY-IW') as reporting_week
      ,metrics.location_allocated
      ,metrics.country_code
      ,nvl(sm.sub_region,'undesignated') as director
      ,nvl(sm.area,'undesignated') as region
      ,metrics.route_type
      ,sum(case when metrics_id = 97 then value else 0 end) as Delivered_Shipments
      ,sum(case when metrics_id =98 then value else 0 end) as LM_hours
      --,sum(case when metrics_id = 6 then 0 else 0 end) as LM_routes --DSP and _DA routes only
      ,sum(case when metrics_id in (99,100) then value else 0 end) as LM_cost
    from 
       _finance.master_metrics_v2 metrics left join _bi_ddl.o_comp_stations_master sm on
      metrics.location_allocated = sm.station_code
      and SM.STATUS IN ('A', 'H')
      and sm.country_code in ('US','CA')
    where
       --reportingdate >= '2017-12-31 00:00:00'
      reportingdate>='2019-01-06'
      and reportingdate <='2019-01-13'
      and provider_type = 'af'
    group by
      reportingdate
      ,metrics.location_allocated
      ,metrics.country_code
      ,provider_type
      ,metrics.route_type
      ,director
      ,region
    )
    select 
      LM_DSP.reportingdate
      ,LM_DSP.location_allocated
      ,LM_DSP.country_code
      ,LM_DSP.route_type
      ,LM_DSP.director
      ,LM_DSP.region
      ,sum(LM_DSP.Delivered_Shipments, LM_DA.Delivered_Shipments, LM_f.Delivered_Shipments) as Delivered_Shipments
      ,sum(LM_DSP.LM_hours,LM_DA.LM_hours,LM_f.LM_hours) as LM_hours
      ,sum(LM_DSP.LM_routes,LM_DA.LM_routes,LM_f.LM_routes) as LM_routes
      ,sum(LM_DSP.LM_cost,LM_DA.LM_cost,LM_f.LM_cost) as LM_cost
      ,LM_DSP.LM_base_cost_dsp
      ,LM_DSP.LM_branding_cost_dsp
      ,LM_DSP.LM_parking_cost_dsp
      ,LM_DSP.LM_fuel_cost_dsp
      ,LM_DSP.LM_fuel_card_cost_dsp
      ,LM_DSP.LM_dispatcher_cost_dsp
      ,LM_DSP.LM_tech_cost_dsp
      ,LM_DSP.LM_deprecation_cost_dsp
      ,LM_DSP.LM_undesignated_cost_dsp
      ,LM_DSP.LM_inperiod_offmanifest_cost_dsp
      ,LM_DSP.LM_outperiod_offmanifest_cost_dsp
      ,LM_DSP.LM_outperiod_manifest_cost_dsp
      ,LM_DSP.LM_monthly_fixed_fee_cost_dsp
      ,LM_DSP.LM_variable_per_piece_cost_dsp
      ,LM_DSP.LM_variable_branding_per_piece_cost_dsp
    from 
      LM_DSP left join LM_DA on LM_DSP.reportingdate = LM_DA.reportingdate
      and LM_DSP.country_code = LM_DA.country_code
      and LM_DSP.location_allocated = LM_DA.location_allocated
      and LM_DSP.route_type = LM_DA.route_type
      left join LM_f on LM_DSP.reportingdate = LM_f.reportingdate
      and LM_DSP.country_code = LM_f.country_code
      and LM_DSP.location_allocated = LM_f.location_allocated
      and LM_DSP.route_type = LM_f.route_type

1 个答案:

答案 0 :(得分:1)

错误消息告诉您,没有功能sum(numeric, numeric, numeric)sum()对集合中组中所有行的表达式求和。如果要在一行中添加值,只需使用+

例如更改

sum(LM_DSP.Delivered_Shipments, LM_DA.Delivered_Shipments, LM_f.Delivered_Shipments) as Delivered_Shipments

收件人:

LM_DSP.Delivered_Shipments + LM_DA.Delivered_Shipments + LM_f.Delivered_Shipments as Delivered_Shipments

...以及所有其他类似的东西。

相关问题