I have the below which gives me SalesExVAT, by BranchNo and by FiscalWeek
there is only 1 record per branch for each week:
Select
sa.BranchNo
,sa.FiscalWeek
,sa.SalesExVAT
From
dbo.SalesAggregateWeek sa
Where
sa.FiscalYear = 2016
I wanted to display this in a Pivoted Format
I have tried the below,
Select
MyData.BranchNo
From
(Select
sa.BranchNo
,sa.FiscalWeek
,sa.SalesExVAT
From
dbo.SalesAggregateWeek sa
Where
sa.FiscalYear = 2016) MyData
Pivot
( sum(MyData.salesexvat)
For
MyData.FiscalWeek In (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52) )
The desired out come would have the FiscalWeek as the headers along the top, the BranchNo displayed down the left, and SalesExVAT info as the data.
Any ideas on what I must do to correct my code are welcome as I've not used PIVOT yet.
Final Query Looks Like:
Select *
From
(Select
sa.BranchNo
,sa.FiscalWeek
,sa.SalesExVAT
From
dbo.SalesAggregateWeek sa
Where
sa.FiscalYear = 2016) P
Pivot
(Sum (SalesExVAT)
For FiscalWeek In
( [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],
[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32],
[33],[34],[35],[36],[37],[38],[39],[40],[41],[42],[43],[44],[45],[46],[47],
[48],[49],[50],[51],[52] )
) As pvt ;
Key points:
The FiscalWeek Values all had to be in [Square Brackets]
The Pivot had to use an alias 'As pvt' and be finished with ;
If anyone knows a way I could have wrote FiscalWeek Between 1 And 52 rather than state all the weeks please comment your answer.
Here an option using a dynamic cross tab pivot...
DECLARE
#WeekColumns VARCHAR(8000) = '',
#sql VARCHAR(8000) = '',
#DeBug BIT = 1; -- change to 0 to execute & 1 to print.
SELECT TOP 52
#WeekColumns = CONCAT(#WeekColumns, ',
[',t.n, '] = SUM(CASE WHEN sa.FiscalWeek = ', t.n, ' THEN sa.SalesExVAT ELSE 0 END)')
FROM
dbo.tfn_Tally(52, 1) t;
SET #sql = CONCAT('
SELECT
sa.BranchNo',
#WeekColumns, '
FROM
dbo.SalesAggregateWeek sa
WHERE
sa.FiscalYear = 2016;')
IF #DeBug = 1
BEGIN
PRINT(#sql);
END;
ELSE
BEGIN
EXEC(#sql);
END;
The print output...
SELECT
sa.BranchNo,
[1] = SUM(CASE WHEN sa.FiscalWeek = 1 THEN sa.SalesExVAT ELSE 0 END),
[2] = SUM(CASE WHEN sa.FiscalWeek = 2 THEN sa.SalesExVAT ELSE 0 END),
[3] = SUM(CASE WHEN sa.FiscalWeek = 3 THEN sa.SalesExVAT ELSE 0 END),
[4] = SUM(CASE WHEN sa.FiscalWeek = 4 THEN sa.SalesExVAT ELSE 0 END),
[5] = SUM(CASE WHEN sa.FiscalWeek = 5 THEN sa.SalesExVAT ELSE 0 END),
[6] = SUM(CASE WHEN sa.FiscalWeek = 6 THEN sa.SalesExVAT ELSE 0 END),
[7] = SUM(CASE WHEN sa.FiscalWeek = 7 THEN sa.SalesExVAT ELSE 0 END),
[8] = SUM(CASE WHEN sa.FiscalWeek = 8 THEN sa.SalesExVAT ELSE 0 END),
[9] = SUM(CASE WHEN sa.FiscalWeek = 9 THEN sa.SalesExVAT ELSE 0 END),
[10] = SUM(CASE WHEN sa.FiscalWeek = 10 THEN sa.SalesExVAT ELSE 0 END),
[11] = SUM(CASE WHEN sa.FiscalWeek = 11 THEN sa.SalesExVAT ELSE 0 END),
[12] = SUM(CASE WHEN sa.FiscalWeek = 12 THEN sa.SalesExVAT ELSE 0 END),
[13] = SUM(CASE WHEN sa.FiscalWeek = 13 THEN sa.SalesExVAT ELSE 0 END),
[14] = SUM(CASE WHEN sa.FiscalWeek = 14 THEN sa.SalesExVAT ELSE 0 END),
[15] = SUM(CASE WHEN sa.FiscalWeek = 15 THEN sa.SalesExVAT ELSE 0 END),
[16] = SUM(CASE WHEN sa.FiscalWeek = 16 THEN sa.SalesExVAT ELSE 0 END),
[17] = SUM(CASE WHEN sa.FiscalWeek = 17 THEN sa.SalesExVAT ELSE 0 END),
[18] = SUM(CASE WHEN sa.FiscalWeek = 18 THEN sa.SalesExVAT ELSE 0 END),
[19] = SUM(CASE WHEN sa.FiscalWeek = 19 THEN sa.SalesExVAT ELSE 0 END),
[20] = SUM(CASE WHEN sa.FiscalWeek = 20 THEN sa.SalesExVAT ELSE 0 END),
[21] = SUM(CASE WHEN sa.FiscalWeek = 21 THEN sa.SalesExVAT ELSE 0 END),
[22] = SUM(CASE WHEN sa.FiscalWeek = 22 THEN sa.SalesExVAT ELSE 0 END),
[23] = SUM(CASE WHEN sa.FiscalWeek = 23 THEN sa.SalesExVAT ELSE 0 END),
[24] = SUM(CASE WHEN sa.FiscalWeek = 24 THEN sa.SalesExVAT ELSE 0 END),
[25] = SUM(CASE WHEN sa.FiscalWeek = 25 THEN sa.SalesExVAT ELSE 0 END),
[26] = SUM(CASE WHEN sa.FiscalWeek = 26 THEN sa.SalesExVAT ELSE 0 END),
[27] = SUM(CASE WHEN sa.FiscalWeek = 27 THEN sa.SalesExVAT ELSE 0 END),
[28] = SUM(CASE WHEN sa.FiscalWeek = 28 THEN sa.SalesExVAT ELSE 0 END),
[29] = SUM(CASE WHEN sa.FiscalWeek = 29 THEN sa.SalesExVAT ELSE 0 END),
[30] = SUM(CASE WHEN sa.FiscalWeek = 30 THEN sa.SalesExVAT ELSE 0 END),
[31] = SUM(CASE WHEN sa.FiscalWeek = 31 THEN sa.SalesExVAT ELSE 0 END),
[32] = SUM(CASE WHEN sa.FiscalWeek = 32 THEN sa.SalesExVAT ELSE 0 END),
[33] = SUM(CASE WHEN sa.FiscalWeek = 33 THEN sa.SalesExVAT ELSE 0 END),
[34] = SUM(CASE WHEN sa.FiscalWeek = 34 THEN sa.SalesExVAT ELSE 0 END),
[35] = SUM(CASE WHEN sa.FiscalWeek = 35 THEN sa.SalesExVAT ELSE 0 END),
[36] = SUM(CASE WHEN sa.FiscalWeek = 36 THEN sa.SalesExVAT ELSE 0 END),
[37] = SUM(CASE WHEN sa.FiscalWeek = 37 THEN sa.SalesExVAT ELSE 0 END),
[38] = SUM(CASE WHEN sa.FiscalWeek = 38 THEN sa.SalesExVAT ELSE 0 END),
[39] = SUM(CASE WHEN sa.FiscalWeek = 39 THEN sa.SalesExVAT ELSE 0 END),
[40] = SUM(CASE WHEN sa.FiscalWeek = 40 THEN sa.SalesExVAT ELSE 0 END),
[41] = SUM(CASE WHEN sa.FiscalWeek = 41 THEN sa.SalesExVAT ELSE 0 END),
[42] = SUM(CASE WHEN sa.FiscalWeek = 42 THEN sa.SalesExVAT ELSE 0 END),
[43] = SUM(CASE WHEN sa.FiscalWeek = 43 THEN sa.SalesExVAT ELSE 0 END),
[44] = SUM(CASE WHEN sa.FiscalWeek = 44 THEN sa.SalesExVAT ELSE 0 END),
[45] = SUM(CASE WHEN sa.FiscalWeek = 45 THEN sa.SalesExVAT ELSE 0 END),
[46] = SUM(CASE WHEN sa.FiscalWeek = 46 THEN sa.SalesExVAT ELSE 0 END),
[47] = SUM(CASE WHEN sa.FiscalWeek = 47 THEN sa.SalesExVAT ELSE 0 END),
[48] = SUM(CASE WHEN sa.FiscalWeek = 48 THEN sa.SalesExVAT ELSE 0 END),
[49] = SUM(CASE WHEN sa.FiscalWeek = 49 THEN sa.SalesExVAT ELSE 0 END),
[50] = SUM(CASE WHEN sa.FiscalWeek = 50 THEN sa.SalesExVAT ELSE 0 END),
[51] = SUM(CASE WHEN sa.FiscalWeek = 51 THEN sa.SalesExVAT ELSE 0 END),
[52] = SUM(CASE WHEN sa.FiscalWeek = 52 THEN sa.SalesExVAT ELSE 0 END)
FROM
dbo.SalesAggregateWeek sa
WHERE
sa.FiscalYear = 2016;
Related
I need to use the "DirectQuery" function on PowerBI Desktop.
When i try to load the data PBI stops the process because of the "SET DATEFIRST 1" instruction at the beginning of the query.
That happens because PBI elaborates the request like 'SELECT * FROM (Myquery)' and that throws a syntax error near "SET".
I'm asking you if there is a way that allows me to set Monday as the first day of the week without using the "SET DATEFIRST 1" instruction.
Here it is the query i'm talking about:
SET DATEFIRST 1;
SELECT SUM(Tabella2.DC20) AS DC20,
SUM(Tabella2.OT20) AS OT20,
SUM(Tabella2.FR20) AS FR20,
SUM(Tabella2.RF20) AS RF20,
SUM(Tabella2.DC40) AS DC40,
SUM(Tabella2.OT40) AS OT40,
SUM(Tabella2.FR40) AS FR40,
SUM(Tabella2.RH40) AS RH40,
SUM(Tabella2.HC40) AS HC40,
SUM(Tabella2.HP40) AS HW40,
SUM(Tabella2.HC45) AS HC45,
SUM(Tabella2.HP45) AS HP45,
SUM(Tabella2.Altri) AS Altri,
Tabella2.YEAR,
Tabella2.month
FROM (SELECT SUM(CASE GET.Abbreviation WHEN '20DV' THEN 1 ELSE 0 END) AS DC20,
SUM(CASE GET.Abbreviation WHEN '20OT' THEN 1 ELSE 0 END) AS OT20,
SUM(CASE GET.Abbreviation WHEN '20FL' THEN 1 ELSE 0 END) AS FR20,
SUM(CASE GET.Abbreviation WHEN '20RE' THEN 1 ELSE 0 END) AS RF20,
SUM(CASE GET.Abbreviation WHEN '40DV' THEN 1 ELSE 0 END) AS DC40,
SUM(CASE GET.Abbreviation WHEN '40OT' THEN 1 ELSE 0 END) AS OT40,
SUM(CASE GET.Abbreviation WHEN '40FL' THEN 1 ELSE 0 END) AS FR40,
SUM(CASE GET.Abbreviation WHEN '40HR' THEN 1 ELSE 0 END) AS RH40,
SUM(CASE GET.Abbreviation WHEN '40HC' THEN 1 ELSE 0 END) AS HC40,
SUM(CASE GET.Abbreviation WHEN '40HP' THEN 1 ELSE 0 END) AS HP40,
SUM(CASE GET.Abbreviation WHEN '45HC' THEN 1 ELSE 0 END) AS HC45,
SUM(CASE GET.Abbreviation WHEN '45HP' THEN 1 ELSE 0 END) AS HP45,
SUM(CASE
WHEN GET.Abbreviation != '20DV'
AND GET.Abbreviation != '20OT'
AND GET.Abbreviation != '20FL'
AND GET.Abbreviation != '20RE'
AND GET.Abbreviation != '40DV'
AND GET.Abbreviation != '40OT'
AND GET.Abbreviation != '40FL'
AND GET.Abbreviation != '40HR'
AND GET.Abbreviation != '40HC'
AND GET.Abbreviation != '40HP'
AND GET.Abbreviation != '45HC'
AND GET.Abbreviation != '45HP' THEN 1
ELSE 0
END) AS Altri,
GV.Vessel_Name,
GVPC.Import_Documentation_Voyage_Number,
GVPC.Actual_Arrival_Time,
DATEPART(MONTH, GVPC.Actual_Arrival_Time) AS month,
DATEPART(YEAR, GVPC.Actual_Arrival_Time) AS YEAR
FROM [My_DB].[dbo].[GISEquipment] GE,
[Interlink_Main90].[dbo].[GISEquipment_Type] GET,
[Interlink_Main90].[dbo].[Equipment_Cycle] EC,
[Interlink_Main90].[dbo].[Equipment_Cycle_Type] ECT,
[Interlink_Main90].[dbo].[Equipment_Event] EE,
[Interlink_Main90].[dbo].[Equipment_Event_Type] EET,
[Interlink_Main90].[dbo].[GISLocation] GL,
[Interlink_Main90].[dbo].[GISPort] GP,
[Interlink_Main90].[dbo].[GISVoyage] GV,
[Interlink_Main90].[dbo].GISVoyage_Port_Call GVPC,
[Interlink_Main90].[dbo].GISVessel GVE
WHERE GE.Equipment_Type_id = GET.Equipment_Type_id
AND GE.Equipment_id = EC.Equipment_id
AND EC.Eq_Cycle_Type_id = ECT.Eq_Cycle_Type_id
AND EC.Eq_Cycle_id = EE.EQ_Cycle_id
AND EE.EQEV_Type_id = EET.EqEv_Type_id
AND EE.Location_id = GL.Location_id
AND EC.POD_id = GP.Port_id
AND EET.Name IN ('IDV')
AND GL.Global_Name = 'Leghorn'
AND GP.Global_Name = 'Leghorn'
AND EE.[Logical_Cancel] = '0'
AND GVPC.Voyage_Port_Call_id = EC.Voyage_Port_Call_id
AND GV.Vessel_id = GVE.Vessel_id
AND GVPC.Voyage_id = GV.Voyage_id
AND GVPC.Port_id = GP.Port_id
AND GV.Logical_Cancel_Value = 0
AND GVPC.Logical_Cancel_Value = 0
AND GVE.Logical_Cancel_Value = 0
AND GVPC.Import_Documentation_Voyage_Number IS NOT NULL
AND GVPC.Actual_Arrival_Time IS NOT NULL
GROUP BY GV.Vessel_Name,
GVPC.Import_Documentation_Voyage_Number,
GVPC.Actual_Arrival_Time) Tabella2
WHERE YEAR = '2022'
GROUP BY Tabella2.YEAR,
Tabella2.month
ORDER BY YEAR DESC,
month;
SET DATEFIRST 7;
Thanks in advance for the help.
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
As the error message tells you, there is no function sum(numeric, numeric, numeric). sum() sums an expression over all rows of a group in an aggregation. Simply use +, when you want to add values across a row.
For example change
sum(LM_DSP.Delivered_Shipments, LM_DA.Delivered_Shipments, LM_f.Delivered_Shipments) as Delivered_Shipments
to:
LM_DSP.Delivered_Shipments + LM_DA.Delivered_Shipments + LM_f.Delivered_Shipments as Delivered_Shipments
... and all the others analogously.
I have the following stored procedure:
select
employeename,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 1 THEN cs.amount ELSE 0 END), 0) as january,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 2 THEN cs.amount ELSE 0 END), 0) as february,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 3 THEN cs.amount ELSE 0 END), 0) as march,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 4 THEN cs.amount ELSE 0 END), 0) as april,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 5 THEN cs.amount ELSE 0 END), 0) as may,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 6 THEN cs.amount ELSE 0 END), 0) as june,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 7 THEN cs.amount ELSE 0 END), 0) as july,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 8 THEN cs.amount ELSE 0 END), 0) as august,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 9 THEN cs.amount ELSE 0 END), 0) as september,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 10 THEN cs.amount ELSE 0 END), 0) as october,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 11 THEN cs.amount ELSE 0 END), 0) as november,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 12 THEN cs.amount ELSE 0 END), 0) as december,
ISNULL(SUM(cs.amount),0) as a_cds
from
statements cs
join
employee mc on mc.employeeid = cs.employeeid
where
cs.chargetype = #chargetype
-- if #accumulate = 1 then SUM all statement amounts from old months
and ((datepart(mm, cs.scheduledate) <= #month and #accumulate = 1)
or (datepart(mm, cs.scheduledate) = #month and #accumulate = 0))
-- if selection is Biweekly (Q) then filter by that
and ((#chargetype = 'Q' and #biweeklypart = 1 and day(cs.scheduledate) = 15) or
(#chargetype = 'Q' and #biweeklypart = 2 and day(cs.scheduledate) <> 15) or
(#chargetype <> 'Q')
)
group by
employeename
Everything works great with this SP but the Accumulate filter. It only works when #chargetype is not 'Q'.
So let's look at the following sample data:
For Employee 15:
#accumulate = 1
#month = 2
#chargetype = 'Q'
#biweeklypart = 2
I should get the SUM of all previous statement records including 02/28/2018. As said this works fine when #chargetype = 'M'.
Any clue?
Hi i have two tables with a lot of data. I have to get some data of both tables. to do this i use an inner join. But when i use SUM i get wrong result. i know that the result is multiplied as many rows returned. How can i get arround this ?
The request :
select SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'cot nette',
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS 'Fond comp',
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'Carte Verte',
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS 'Adhesion',
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe',
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Fond Comp',
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Adhésion',
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe carte verte',
SUM(hst.Q_Access) as 'Access'
from OtoHistorique hst
inner Join OtoHistoriqueDet det On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086
i get 60 in Access column when the expected result is 20
Anyone can help me with this ? Sorry if i use bad syntax in my request but i'm just a beginner in SQL and for the moment the result is more important than the syntax.
You probably have one-to-many relationship between the tables, which means some of the values for SUM get duplicated.
You can go around this by avoiding the join like this:
select SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'cot nette',
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS 'Fond comp',
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS 'Carte Verte',
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS 'Adhesion',
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe',
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Fond Comp',
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe Adhésion',
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS 'Taxe carte verte',
(SELECT SUM(hst.Q_Access)
FROM OtoHistorique hst
WHERE det.IdHistorique = hst.IdHistorique) as 'Access'
FROM OtoHistoriqueDet det
WHERE EXISTS (SELECT 1
FROM OtoHistorique hst2
WHERE hst2.POLICE = 3221086
AND hst2.IdHistorique = det.IdHistorique)
This could probably be more optimized but we'd need more info on the logic between the two tables.
One approach is to do the aggregation before the join:
select det.*,
SUM(hst.Q_Access) as "Access"
from OtoHistorique hst inner Join
(select det.IdHistoric,
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) as "cot nette"
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS "Fond comp",
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS "Carte Verte",
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS "Adhesion",
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe",
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Fond Comp",
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Adhésion",
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe carte verte",
SUM(hst.Q_Access) as "Access"
from OtoHistoriqueDet det
group by det.IdHistorique
) det
On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086;
Because you are filtering the query, this might be expensive. The entire details table has to be aggregated. So, you can do the join in the subquery for filtering purposes and then again for the calculation:
select det.*,
SUM(hst.Q_Access) as "Access"
from OtoHistorique hst inner Join
(select det.IdHistoric,
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_PrimeNette ELSE 0 END) as "cot nette"
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_PrimeNette ELSE 0 END) AS "Fond comp",
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_PrimeNette ELSE 0 END) AS "Carte Verte",
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_PrimeNette ELSE 0 END) AS "Adhesion",
SUM(CASE WHEN det.IdGarantie != 18 AND det.IdGarantie != 17 AND det.IdGarantie!= 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe",
SUM(CASE WHEN det.IdGarantie = 18 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Fond Comp",
SUM(CASE WHEN det.IdGarantie = 17 THEN det.Q_Taxe ELSE 0 END) AS "Taxe Adhésion",
SUM(CASE WHEN det.IdGarantie = 11 THEN det.Q_Taxe ELSE 0 END) AS "Taxe carte verte"
from OtoHistoriqueDet det join
OtoHistorique hst
On det.IdHistorique = hst.IdHistorique and hst.POLICE = 3221086
group by det.IdHistorique
) det
On det.IdHistorique = hst.IdHistorique
where hst.POLICE = 3221086;
I have the following 2 queries:
Query 1:
WITH JobTransactionsSumTypes AS
(
SELECT
[Job],
[Cost_Code],
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] <>'' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountComSum,
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] ='' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountNonComSum,
SUM(CASE WHEN [Transaction_Type] = 'Approved est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ApprovedEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd schdl val chn' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdSchdlValChnAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Cash receipt' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CashReceiptAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Committed cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CommittedCostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Dollars paid' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS DollarsPaidAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'General Ledger only' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS GeneralLedgerOnlyAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'JC cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS JCcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 1' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet1AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 4' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet4AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Original estimate' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS OriginalEstimateAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pending est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PendingEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Percent complete' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PercentCompleteAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schdl val chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdlValChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'PR cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PRcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Receivable Adjstment' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ReceivableAdjstmentAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Retention billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS RetentionBilledAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Scheduled value' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ScheduledValueAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Work Billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS WorkBilledAmountSum
FROM [ADCData_Doric].[dbo].[JCT_CURRENT__TRANSACTION]
WHERE
GROUP BY
[Job],
[Cost_Code]
)
SELECT
Job,
Cost_Code,
APcostAmountComSum,
APcostAmountNonComSum,
ApprovedEstChangesAmountSum,
AprvdCmmttCstChngAmountSum,
AprvdSchdlValChnAmountSum,
CashReceiptAmountSum,
CommittedCostAmountSum,
DollarsPaidAmountSum,
GeneralLedgerOnlyAmountSum,
JCcostAmountSum,
MiscWorksheet1AmountSum,
MiscWorksheet2AmountSum,
MiscWorksheet3AmountSum,
MiscWorksheet4AmountSum,
OriginalEstimateAmountSum,
PendingEstChangesAmountSum,
PercentCompleteAmountSum,
PndngCmmttCstChngAmountSum,
PndngSchdValChg2AmountSum,
PndngSchdValChg3AmountSum,
PndngSchdlValChngAmountSum,
PRcostAmountSum,
ReceivableAdjstmentAmountSum,
RetentionBilledAmountSum,
ScheduledValueAmountSum,
WorkBilledAmountSum,
(ScheduledValueAmountSum) AS StartContractAmount,
(AprvdSchdlValChnAmountSum) AS ApprovedVariations,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum + PendingEstChangesAmountSum) AS TotalBudgetOriginalApprovedPending,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum)*(MiscWorksheet1AmountSum/100)+(PendingEstChangesAmountSum*(MiscWorksheet2AmountSum/100)) AS TotalCommitmentsBudget,
(CommittedCostAmountSum + AprvdCmmttCstChngAmountSum + PndngCmmttCstChngAmountSum) AS ApprovedPendingCommitedCosts
-- You Could Add additional Calculations here.
FROM
[JobTransactionsSumTypes]
Query 2:
SELECT
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status <>0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS OnHoldAmount
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status =0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS NotOnHoldAmount
FROM
dbo.JCT_CURRENT__TRANSACTION JCT_CURRENT__TRANSACTION
LEFT OUTER JOIN dbo.APM_MASTER__DISTRIBUTION APM_MASTER__DISTRIBUTION
ON JCT_CURRENT__TRANSACTION.Vendor = APM_MASTER__DISTRIBUTION.Vendor AND JCT_CURRENT__TRANSACTION.Invoice = APM_MASTER__DISTRIBUTION.Invoice AND JCT_CURRENT__TRANSACTION.Dist_Sequence = APM_MASTER__DISTRIBUTION.Dist_Seq
GROUP BY
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
I can join these in Crystal reports, but would like to move this into a single query through left outer joining (getting all Query 1 Results) and linking through the job and cost code to query 2.
I suppose what I am asking is, Query 1 uses a common table expression but query 2 does not, and i'm not sure how to join them.
Thanks.
You can use multiple CTE in one query
Simple example:
;WITH cte1 AS
(
SELECT *
FROM table1
), AS cte2
(
SELECT *
FROM table2
)
SELECT *
FROM cte1 c1 LEFT JOIN cte2 c2 ON c1.id = c2.id
Your query with multiple CTE
;WITH JobTransactionsSumTypes AS
(
SELECT
[Job],
[Cost_Code],
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] <>'' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountComSum,
SUM(CASE WHEN [Transaction_Type] = 'AP cost' AND [Commitment] ='' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS APcostAmountNonComSum,
SUM(CASE WHEN [Transaction_Type] = 'Approved est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ApprovedEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Aprvd schdl val chn' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS AprvdSchdlValChnAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Cash receipt' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CashReceiptAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Committed cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS CommittedCostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Dollars paid' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS DollarsPaidAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'General Ledger only' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS GeneralLedgerOnlyAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'JC cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS JCcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 1' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet1AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Misc worksheet 4' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS MiscWorksheet4AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Original estimate' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS OriginalEstimateAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pending est changes' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PendingEstChangesAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Percent complete' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PercentCompleteAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng cmmtt cst chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngCmmttCstChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 2' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg2AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schd val chg 3' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdValChg3AmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Pndng schdl val chng' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PndngSchdlValChngAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'PR cost' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS PRcostAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Receivable Adjstment' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ReceivableAdjstmentAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Retention billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS RetentionBilledAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Scheduled value' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS ScheduledValueAmountSum,
SUM(CASE WHEN [Transaction_Type] = 'Work Billed' THEN [Amount]-[Noncosted_Tax] ELSE 0 END) AS WorkBilledAmountSum
FROM [ADCData_Doric].[dbo].[JCT_CURRENT__TRANSACTION]
--WHERE your_condition
GROUP BY
[Job],
[Cost_Code]
), JobTransactionsSumTypes_Query2 AS
(
SELECT
APM_MASTER__DISTRIBUTION.Job
, APM_MASTER__DISTRIBUTION.Cost_Code
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status <>0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS OnHoldAmount
,SUM(CASE WHEN APM_MASTER__DISTRIBUTION.Onhold_Status =0 THEN JCT_CURRENT__TRANSACTION.Amount-JCT_CURRENT__TRANSACTION.Noncosted_Tax ELSE 0 END) AS NotOnHoldAmount
FROM
dbo.JCT_CURRENT__TRANSACTION JCT_CURRENT__TRANSACTION
LEFT OUTER JOIN dbo.APM_MASTER__DISTRIBUTION APM_MASTER__DISTRIBUTION
ON JCT_CURRENT__TRANSACTION.Vendor = APM_MASTER__DISTRIBUTION.Vendor AND JCT_CURRENT__TRANSACTION.Invoice = APM_MASTER__DISTRIBUTION.Invoice AND JCT_CURRENT__TRANSACTION.Dist_Sequence = APM_MASTER__DISTRIBUTION.Dist_Seq
GROUP BY
APM_MASTER__DISTRIBUTION.Job
,APM_MASTER__DISTRIBUTION.Cost_Code
)
SELECT
Job,
Cost_Code,
APcostAmountComSum,
APcostAmountNonComSum,
ApprovedEstChangesAmountSum,
AprvdCmmttCstChngAmountSum,
AprvdSchdlValChnAmountSum,
CashReceiptAmountSum,
CommittedCostAmountSum,
DollarsPaidAmountSum,
GeneralLedgerOnlyAmountSum,
JCcostAmountSum,
MiscWorksheet1AmountSum,
MiscWorksheet2AmountSum,
MiscWorksheet3AmountSum,
MiscWorksheet4AmountSum,
OriginalEstimateAmountSum,
PendingEstChangesAmountSum,
PercentCompleteAmountSum,
PndngCmmttCstChngAmountSum,
PndngSchdValChg2AmountSum,
PndngSchdValChg3AmountSum,
PndngSchdlValChngAmountSum,
PRcostAmountSum,
ReceivableAdjstmentAmountSum,
RetentionBilledAmountSum,
ScheduledValueAmountSum,
WorkBilledAmountSum,
(ScheduledValueAmountSum) AS StartContractAmount,
(AprvdSchdlValChnAmountSum) AS ApprovedVariations,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum + PendingEstChangesAmountSum) AS TotalBudgetOriginalApprovedPending,
(OriginalEstimateAmountSum + ApprovedEstChangesAmountSum)*(MiscWorksheet1AmountSum/100)+(PendingEstChangesAmountSum*(MiscWorksheet2AmountSum/100)) AS TotalCommitmentsBudget,
(CommittedCostAmountSum + AprvdCmmttCstChngAmountSum + PndngCmmttCstChngAmountSum) AS ApprovedPendingCommitedCosts,
j2.OnHoldAmount,
j2.NotOnHoldAmount
FROM
[JobTransactionsSumTypes] j1 LEFT OUTER JOIN JobTransactionsSumTypes_Query2 j2
ON j1.Job = j2.Job AND j1.Cost_Code = j2.Cost_Code