1

我有一个相当复杂的多个嵌套查询集,我需要将其聚合到一个视图中,但我不断收到一个指标不存在错误。每个组件都运行良好,但是当我将它们全部放在一起时,我得到一个错误,指出操作无效:函数 sum(numeric, numeric, numeric) 不存在

任何帮助将不胜感激

 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
4

1 回答 1

1

正如错误消息告诉您的那样,没有 function 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

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

于 2019-01-20T02:56:35.940 回答