I have a fairly complicated multiple nested query set that I need to aggregate in to one view but I keep getting a metric does not exist error. Each component piece runs fine but when I bring them all together I get and error that states Invalid operation: function sum(numeric, numeric, numeric) does not exist
Any help would be greatly appreciated
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