Join a Query with another Query while using a common table expression - sql

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

Related

How can I avoid using "SET DATEFIRST 1" instruction?

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.

Sql query to calculate count of email sent from email open and email bounceback

So basically i have to calculate sents against subject line , Here is the query i have used:
Select distinct SubjectLine
,case when ([Activity] in ('Click','Opted In','Unsubscribe - All')) then count([EmailAddress]) else 0 end as 'TotalCT'
,case when ([Activity] in ('Click','Opted In','Unsubscribe - All')) then count(distinct[EmailAddress]) else 0 end as 'UniqueCT'
,case when ([Activity] = 'Open') then count([EmailAddress] ) else 0 end as 'TotalOpens'
,case when ([Activity] = 'Open') then count(distinct[EmailAddress]) else 0 end as 'UniqueOpens'
,case when ( [Activity] = 'Bounceback' ) then count([EmailAddress]) else 0 end as 'Bounces'
,case when ([Activity] in ('Open','Bounceback'))then count([EmailAddress]) else 0 end as 'Sends'
from xyz
Group by SubjectLine, Activity
I think you want conditional aggregation:
Select SubjectLine
sum(case when [Activity] in ('Click', 'Opted In', 'Unsubscribe - All') then 1 else 0) end as TotalCT,
sum(case when [Activity] in ('Click', 'Opted In', 'Unsubscribe - All') then 1 else 0 end) as UniqueCT,
sum(case when [Activity] = 'Open' then 1 else 0 end) as TotalOpens,
sum(case when [Activity] = 'Open' then 1 else 0 end) as UniqueOpens,
sum(case when [Activity] = 'Bounceback' then 1 else 0 end) as Bounces,
sum(case when [Activity] in ('Open', 'Bounceback') then 1 else 0 end) as Sends
from xyz
Group by SubjectLine;

Getting error of "Invalid column name '2017-09'.." while run sql query

When i run below query, It gives me error Invalid column name '2017-09'.,
I tried some googling but still not getting the result for it, can anyone please look into this query and help me what can be the issue
SELECT
COUNT(CASE when (SOC = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as SOCCount_2017_09,
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertCount_2017_09,
COUNT(CASE when (Recert = "1" and MonthStart="2017-09") then 1 ELSE NULL END) as RecertPer1_2017_09,
COUNT(CASE when (MonthStart="2017-09") then 1 ELSE NULL END) as TotalEpisode_2017_09,
ROUND(AVG(CASE when (FullHHRG > "0" and MonthStart="2017-09") then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > "0" and MonthStart="2017-09") then CMW ELSE NULL END),3) as AvgCMW_2017_09,
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAs_2017_09,
COUNT(CASE when (LUPA = "1" and MonthEnd="2017-09") then 1 ELSE NULL END) as LUPAPer_2017_09,
COUNT(CASE when ((MonthEnd="2017-09" and Status = "Closed") ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09,
ROUND(AVG(CASE when (SN_Ep = "1" and MonthEnd="2017-09") then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09,
ROUND(AVG(CASE when (THVisits > "0" and MonthEnd="2017-09") then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09,
ROUND(AVG(CASE when (Status = "Closed" and MonthEnd="2017-09") then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09,
ROUND(AVG(CASE when (NetHHRG > "0" and MonthEnd="2017-09") then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09,
SUM(CASE when (Status = "Closed" and MonthEnd="2017-09") then TH_Ep ELSE NULL END) as TherapyPer_2017_09
FROM tb_Episode WHERE CustID = "27" AND PayerType = "Ep" AND BranchID IN (241)
In SQL-Server, You have to use Single Quotes(') Instead of Double Quotes(") while using Strings.
Try this:
SELECT
COUNT(CASE when (SOC = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as SOCCount_2017_09,
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertCount_2017_09,
COUNT(CASE when (Recert = '1' and MonthStart='2017-09') then 1 ELSE NULL END) as RecertPer1_2017_09,
COUNT(CASE when (MonthStart='2017-09') then 1 ELSE NULL END) as TotalEpisode_2017_09,
ROUND(AVG(CASE when (FullHHRG > '0' and MonthStart='2017-09') then FullHHRG ELSE NULL END),2) as AvgFullHHRG_2017_09,
ROUND(AVG(CASE when (CMW > '0' and MonthStart='2017-09') then CMW ELSE NULL END),3) as AvgCMW_2017_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAs_2017_09,
COUNT(CASE when (LUPA = '1' and MonthEnd='2017-09') then 1 ELSE NULL END) as LUPAPer_2017_09,
COUNT(CASE when ((MonthEnd='2017-09' and Status = 'Closed') ) then 1 ELSE NULL END) as TotalEndEpisode_2017_09,
ROUND(AVG(CASE when (SN_Ep = '1' and MonthEnd='2017-09') then SN_Visits ELSE NULL END),1) as SNVisitsSNEps_2017_09,
ROUND(AVG(CASE when (THVisits > '0' and MonthEnd='2017-09') then THVisits ELSE NULL END),1) as THVisitsTHEps_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotVisits ELSE NULL END),1) as AvgTotVisits_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then SNVisits ELSE NULL END),1) as AvgSNVisits_2017_09,
ROUND(AVG(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TotTherapy ELSE NULL END),1) as AvgTHVisits_2017_09,
ROUND(AVG(CASE when (NetHHRG > '0' and MonthEnd='2017-09') then NetHHRG ELSE NULL END),2) as AvgNetHHRG_2017_09,
SUM(CASE when (Status = 'Closed' and MonthEnd='2017-09') then TH_Ep ELSE NULL END) as TherapyPer_2017_09
FROM tb_Episode WHERE CustID = '27' AND PayerType = 'Ep' AND BranchID IN (241)

Can't get union to work

I have a rather long query, and I got it to work, kind of. I can;t get the two unions to work since both have different FROM clauses (I THINK)
I think it would be best to post most of it because it is very convoluted. sorry if it takes up so much space.
SELECT
ISNULL(#tempBacklog.Division, #tempCompleteNotInvoiced.Division) AS 'Division',
'NONE' as 'Division ID',
'NONE' as 'NAME',
'NONE' AS 'Sales Engineer',
0.00 As 'Current MTD SALES',
0.00 As 'Last Year MTD SALES',
0.00 as 'Projected MTD Sales',
ISNULL(Backlog, 0) AS 'Backlog',
ISNULL([Complete Not Invoiced], 0) AS 'Completed Not Invoiced',
ISNULL(#tempBacklog.CUSTACCOUNT, #tempCompleteNotInvoiced.CUSTACCOUNT) AS 'accountnum'
FROM
--#tempSales FULL OUTER JOIN
#tempBacklog FULL OUTER JOIN
#tempCompleteNotInvoiced ON #tempBacklog.ITEMGROUPID = #tempCompleteNotInvoiced.ITEMGROUPID
AND #tempBacklog.Division = #tempCompleteNotInvoiced.Division
AND #tempBacklog.[Ledger Account] = #tempCompleteNotInvoiced.[Ledger Account]
AND #tempBacklog.CUSTACCOUNT = #tempCompleteNotInvoiced.CUSTACCOUNT
union all
--Temp table for JE voucher entries , because of dupilacte entries in Custinvoice and CustInoviceJour table
select Division,
[Division ID],
[NAME],
CASE WHEN IsNull([Sales Engineer],'')='' Then Division+'-UnKnown' ELSE [Sales Engineer] END AS 'Sales Engineer',
(case WHEN MONTH(GETDATE()) = 1 then SUM([1])
WHEN MONTH(GETDATE()) = 2 then SUM([2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([12])
END) As 'Current MTD SALES',
(case WHEN MONTH(GETDATE()) = 1 then SUM([L1])
WHEN MONTH(GETDATE()) = 2 then SUM([L2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([L3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([L4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([L5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([L6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([L7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([L8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([L9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([L10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([L11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([L12])
END) As 'Last Year MTD SALES',
(case WHEN MONTH(GETDATE()) = 1 then SUM([YTD1])
WHEN MONTH(GETDATE()) = 2 then SUM([YTD2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([YTD3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([YTD4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([YTD5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([YTD6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([YTD7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([YTD8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([YTD9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([YTD10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([YTD11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([YTD12])
END) as 'Projected MTD Sales',
SUM(Backlog) AS 'Backlog',
SUM([Completed Not Invoiced]) AS 'Completed Not Invoiced'
,accountnum
from
(
select
IsNULL([DESCRIPTION],'None') as Division,
LT.[DIMENSION2_] AS 'Division ID',
'NONE' as 'NAME',
'NONE' as 'ACCOUNTNUM',
'NONE' AS 'Industry',
'NONE' as 'Street',
'NONE' as 'City',
'NONE' as 'State',
'NONE' as 'ZipCode',
'' as 'Sales Engineer',--IsNULL([DESCRIPTION],'None')+'-UnKnown' AS 'Sales Engineer',
'NONE' as 'SalesGroup',
'' as 'SALESMANID',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
0.00 as 'Projection',
0.00 AS 'YTD Projection',
0.00 AS 'YTD1',
0.00 AS 'YTD2',
0.00 AS 'YTD3',
0.00 AS 'YTD4',
0.00 AS 'YTD5',
0.00 AS 'YTD6',
0.00 AS 'YTD7',
0.00 AS 'YTD8',
0.00 AS 'YTD9',
0.00 AS 'YTD10',
0.00 AS 'YTD11',
0.00 AS 'YTD12',
0.00 as 'Year Goal',
'' as 'PROJECTION COMMENT'
,0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from dbo.[LedgerTrans] LT
inner Join dbo.[LedgerJournalTrans] LJ on LT.Voucher = LJ.Voucher and LT.TransDate = LJ.TransDate and AccountType = '0'
and LJ.ACCOUNTNUM = LT.ACCOUNTNUM and LT.DIMENSION2_=LJ.DIMENSION2_
and LT.AMOUNTMST = (CASE WHEN AMOUNTCURDEBIT != 0 then AMOUNTCURDEBIT else -1*AMOUNTCURCREDIT END)
Inner JOIN dbo.DIMENSIONS ON NUM = LT.DIMENSION2_
Inner Join dbo.LedgerTable LD on LD.ACCOUNTNUM = LJ.ACCOUNTNUM and LD.ACCOUNTNUM = LT.ACCOUNTNUM
--inner join #tempTotals on LJ.accountnum = #temptotals.[Ledger Account]
where (#reportType = 'Forecasting' and LJ.ACCOUNTNUM between '400100' and '409400' and LT.ACCOUNTNUM between '400100' and '409400' and LJ.DocumentNum = ''
and LT.[DIMENSION2_] != '10' and LD.KTI_OASALESCOMM = 0
AND LT.[DIMENSION2_] in (#dimension))
or
(#reportType = 'Financial' and LJ.DocumentNum = ''
and LT.[DIMENSION2_] != '10'
and LJ.ACCOUNTNUM between '400100' and '409400'
and LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension))
group by
[DESCRIPTION],LT.[DIMENSION2_]
UNION ALL
select
IsNULL(ISNULL(NULLIF(d2.[DESCRIPTION], ''), d1.[DESCRIPTION]),'None') as Division,
IsNULL(ISNULL(NULLIF(d2.NUM, ''), d1.NUM),'None') as 'Division ID',
cu.[NAME] as 'NAME',
cu.[ACCOUNTNUM] as 'ACCOUNTNUM',
BUSINESSSECTORID AS 'Industry',
cu.[STREET] as 'Street',
cu.[CITY] as 'City',
cu.[STATE] as 'State',
cu.[ZIPCODE] as 'ZipCode',
[Sales Engineer] as 'Sales Engineer',--IsNULL([DESCRIPTION],'None')+'-UnKnown' AS 'Sales Engineer',
cu.[SalesGroup] as 'SalesGroup',
IsNull(tes.SALESMANID,'') as 'SALESMANID',
SUM([YTD Sales]) as 'YTD Sales',SUM([1]) as '1',SUM([2]) as '2',SUM([3]) as '3',SUM([4]) as '4',SUM([5]) as '5'
,SUM([6]) as '6',SUM([7]) as '7',SUM([8]) as '8',SUM([9]) as '9',SUM([10]) as '10',SUM([11]) as '11',SUM([12]) as '12',
SUM([Last YTD Sales]) as 'Last YTD Sales',SUM([Last Year Sales]) as 'Last Year Sales',
SUM([L1]) as 'L1',SUM([L2]) as 'L2',SUM([L3]) as 'L3',SUM([L4]) as 'L4',SUM([L5]) as 'L5'
,SUM([L6]) as 'L6',SUM([L7]) as 'L7',SUM([L8]) as 'L8',SUM([L9]) as 'L9',SUM([L10]) as 'L10',
SUM([L11]) as 'L11',SUM([L12]) as 'L12',
IsNULL(Projection,0.00) as 'Projection',
[YTD Projection] as 'YTD Projection',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#1YTDStart and ENDDATE<=#1YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD1',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#2YTDStart and ENDDATE<=#2YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD2',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#3YTDStart and ENDDATE<=#3YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD3',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#4YTDStart and ENDDATE<=#4YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD4',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#5YTDStart and ENDDATE<=#5YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD5',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#6YTDStart and ENDDATE<=#6YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD6',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#7YTDStart and ENDDATE<=#7YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD7',
ISNULL((Select Top(1)[YTD Projection] from #tempSalesTargetMW where STARTDATE>=#8YTDStart and ENDDATE<=#8YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD8',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#9YTDStart and ENDDATE<=#9YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD9',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#10YTDStart and ENDDATE<=#10YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD10',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#11YTDStart and ENDDATE<=#11YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD11',
ISNULL((Select Top(1) [YTD Projection] from #tempSalesTargetMW where STARTDATE>=#12YTDStart and ENDDATE<=#12YTDEnd and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null) and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD12',
[Year Goal] as 'Year Goal',[PROJECTION COMMENT],
0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from [CUSTTABLE] cu
Left JOIN
(
select
LT.[DIMENSION2_] AS 'Division ID',
LJ.ACCOUNTNUM as 'ACCOUNTNUM',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
LT.[TRANSDATE]
,0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
dbo.[LedgerTrans] LT
inner JOIN dbo.[LedgerJournalTrans] LJ on LT.Voucher = LJ.Voucher and LT.TransDate = LJ.TransDate and AccountType in ('1')
Inner Join dbo.LedgerTable LD on LD.ACCOUNTNUM = LT.ACCOUNTNUM
where
(LT.ACCOUNTNUM between '400100' and '409400')
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End and LT.[DIMENSION2_] = '10' and LT.Voucher Not Like 'JE%'
and LD.KTI_OASALESCOMM = 0 AND LT.[DIMENSION2_] in (#dimension) and #reportType='Forecasting'
or
(LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End and LT.[DIMENSION2_] = '10' and LT.Voucher Not Like 'JE%'
AND LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension) and #reportType='Financial')
group by LJ.ACCOUNTNUM,LT.[DIMENSION2_],LT.[TRANSDATE]
union all
select
LT.[DIMENSION2_] AS 'Division ID',
LJ.ACCOUNTNUM as 'ACCOUNTNUM',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
LT.[TRANSDATE]
,0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
dbo.[LedgerTrans] LT
inner JOIN dbo.[LedgerJournalTrans] LJ on LT.Voucher = LJ.Voucher and LT.TransDate = LJ.TransDate and AccountType in ('1')
Inner Join dbo.LedgerTable LD on LD.ACCOUNTNUM = LT.ACCOUNTNUM
where
(LT.ACCOUNTNUM between '400100' and '409400')
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End and LT.[DIMENSION2_] != '10' and LD.KTI_OASALESCOMM = 0 and #reportType = 'Forecasting'
AND LT.[DIMENSION2_] in (#dimension)
or
(LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End and LT.[DIMENSION2_] != '10' and #reportType = 'Financial'
AND LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension))
group by LJ.ACCOUNTNUM,LT.[DIMENSION2_],LT.[TRANSDATE]
) as Cus on (cu.ACCOUNTNUM = Cus.ACCOUNTNUM)
Inner JOIN dbo.DIMENSIONS d1 ON NUM = cu.[dimension2_]
LEFT OUTER JOIN dbo.DIMENSIONS d2 ON d2.NUM = Cus.[Division ID]
Left JOIN #tempSalesTarget te on cu.ACCOUNTNUM=te.CUSTACCOUNT
and te.Division=Cus.[Division ID]
and cu.SALESGROUP=te.SALESUNITID
left JOIN #tempSalesEngineers tes ON cu.SALESGROUP=tes.SALESUNITID and (te.SALESMANID = tes.SALESMANID or te.SalesManID IS NULL)
LEFT OUTER JOIN #tempCustIndustry ON cu.ACCOUNTNUM = #tempCustIndustry.ACCOUNTNUM
where IsNull(tes.SALESMANID,'') IN (#SalesEngineer)
AND cu.[dimension2_] in (#dimension)
GROUP BY
d1.[DESCRIPTION],
d2.[DESCRIPTION],
d1.NUM,
d2.NUM,
cu.[NAME],
cu.[ACCOUNTNUM],
BUSINESSSECTORID,
cu.[STREET],
cu.[CITY],
cu.[STATE],
cu.[ZIPCODE],
cu.[SalesGroup],
[Sales Engineer],
tes.SALESMANID,
[Projection],
[Year Goal],
[YTD Projection],
[PROJECTION COMMENT],
backlog
I am getting two different results for each entry:
How can I get the rows to match up? the fields for the first query are invalid in the second query
I could be wrong, but it appears your columns dont match... your first query has a column
ISNULL(#tempBacklog.CUSTACCOUNT, #tempCompleteNotInvoiced.CUSTACCOUNT) AS 'accountnum'
but there is no corresponding (last column in the second query). A union needs to be same columns, data types and order. I think it was just an oversight. For grins, even if in the second query you add
'test' as 'accountnum'
just to confirm, see what happens / if it goes through.
ADDITIONALLY, the last column of your second query looks cut short via...
SUM([Completed Not Invoiced]) AS 'Completed Not Invoiced'
) as Cus on (cu.ACCOUNTNUM = Cus.ACCOUNTNUM)
As menstioned, no 'accountnum' column to match the first query, but also not seeing a "FROM" clause and it's initial table before the JOIN. Again. maybe I'm missing something, but the formatting of the query is not quite there.
I have reformatted the query some for readability from your original. I changed some of the long #temp aliases to abbreviated for readability purposes.
SELECT
ISNULL(tBL.Division, #tempCompleteNotInvoiced.Division) AS 'Division',
'NONE' as 'Division ID',
'NONE' as 'NAME',
'NONE' AS 'Sales Engineer',
0.00 As 'Current MTD SALES',
0.00 As 'Last Year MTD SALES',
0.00 as 'Projected MTD Sales',
ISNULL(Backlog, 0) AS 'Backlog',
ISNULL([Complete Not Invoiced], 0) AS 'Completed Not Invoiced',
ISNULL(tBL.CUSTACCOUNT, #tempCompleteNotInvoiced.CUSTACCOUNT) AS 'accountnum'
FROM
#tempBacklog tBL
FULL OUTER JOIN #tempCompleteNotInvoiced
ON tBL.ITEMGROUPID = tCNI.ITEMGROUPID
AND tBL.Division = tCNI.Division
AND tBL.[Ledger Account] = tCNI.[Ledger Account]
AND tBL.CUSTACCOUNT = tCNI.CUSTACCOUNT
union all
select
Division,
[Division ID],
[NAME],
CASE WHEN IsNull([Sales Engineer],'')=''
Then Division+'-UnKnown'
ELSE [Sales Engineer] END AS 'Sales Engineer',
(case WHEN MONTH(GETDATE()) = 1 then SUM([1])
WHEN MONTH(GETDATE()) = 2 then SUM([2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([12])
END) As 'Current MTD SALES',
(case WHEN MONTH(GETDATE()) = 1 then SUM([L1])
WHEN MONTH(GETDATE()) = 2 then SUM([L2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([L3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([L4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([L5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([L6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([L7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([L8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([L9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([L10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([L11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([L12])
END) As 'Last Year MTD SALES',
(case WHEN MONTH(GETDATE()) = 1 then SUM([YTD1])
WHEN MONTH(GETDATE()) = 2 then SUM([YTD2])
WHEN MONTH(GETDATE()) = 3 THEN SUM([YTD3])
WHEN MONTH(GETDATE()) = 4 THEN SUM([YTD4])
WHEN MONTH(GETDATE()) = 5 THEN SUM([YTD5])
WHEN MONTH(GETDATE()) = 6 THEN SUM([YTD6])
WHEN MONTH(GETDATE()) = 7 THEN SUM([YTD7])
WHEN MONTH(GETDATE()) = 8 THEN SUM([YTD8])
WHEN MONTH(GETDATE()) = 9 THEN SUM([YTD9])
WHEN MONTH(GETDATE()) = 10 THEN SUM([YTD10])
WHEN MONTH(GETDATE()) = 11 THEN SUM([YTD11])
WHEN MONTH(GETDATE()) = 12 THEN SUM([YTD12])
END) as 'Projected MTD Sales',
SUM(Backlog) AS 'Backlog',
SUM([Completed Not Invoiced]) AS 'Completed Not Invoiced',
accountnum
from
( select
IsNULL([DESCRIPTION],'None') as Division,
LT.[DIMENSION2_] AS 'Division ID',
'NONE' as 'NAME',
'NONE' as 'ACCOUNTNUM',
'NONE' AS 'Industry',
'NONE' as 'Street',
'NONE' as 'City',
'NONE' as 'State',
'NONE' as 'ZipCode',
'' as 'Sales Engineer',
'NONE' as 'SalesGroup',
'' as 'SALESMANID',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
0.00 as 'Projection',
0.00 AS 'YTD Projection',
0.00 AS 'YTD1',
0.00 AS 'YTD2',
0.00 AS 'YTD3',
0.00 AS 'YTD4',
0.00 AS 'YTD5',
0.00 AS 'YTD6',
0.00 AS 'YTD7',
0.00 AS 'YTD8',
0.00 AS 'YTD9',
0.00 AS 'YTD10',
0.00 AS 'YTD11',
0.00 AS 'YTD12',
0.00 as 'Year Goal',
'' as 'PROJECTION COMMENT',
0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
dbo.[LedgerTrans] LT
inner Join dbo.[LedgerJournalTrans] LJ
on LT.Voucher = LJ.Voucher
and LT.TransDate = LJ.TransDate
and AccountType = '0'
and LT.ACCOUNTNUM = LJ.ACCOUNTNUM
and LT.DIMENSION2_ = LJ.DIMENSION2_
and LT.AMOUNTMST = (CASE WHEN AMOUNTCURDEBIT != 0
then AMOUNTCURDEBIT
else -1 * AMOUNTCURCREDIT END)
Inner JOIN dbo.DIMENSIONS
ON LT.DIMENSION2_ = NUM
Inner Join dbo.LedgerTable LD
ON LT.ACCOUNTNUM = LD.ACCOUNTNUM
AND LJ.ACCOUNTNUM = LD.ACCOUNTNUM
where
( #reportType = 'Forecasting'
and LJ.ACCOUNTNUM between '400100' and '409400'
and LT.ACCOUNTNUM between '400100' and '409400'
and LJ.DocumentNum = ''
and LT.[DIMENSION2_] != '10'
and LD.KTI_OASALESCOMM = 0
AND LT.[DIMENSION2_] in (#dimension))
OR
( #reportType = 'Financial'
and LJ.DocumentNum = ''
and LT.[DIMENSION2_] != '10'
and LJ.ACCOUNTNUM between '400100' and '409400'
and LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension))
group by
[DESCRIPTION],
LT.[DIMENSION2_]
UNION ALL
SELECT
IsNULL(ISNULL(NULLIF(d2.[DESCRIPTION], ''), d1.[DESCRIPTION]),'None') as Division,
IsNULL(ISNULL(NULLIF(d2.NUM, ''), d1.NUM),'None') as 'Division ID',
cu.[NAME] as 'NAME',
cu.[ACCOUNTNUM] as 'ACCOUNTNUM',
BUSINESSSECTORID AS 'Industry',
cu.[STREET] as 'Street',
cu.[CITY] as 'City',
cu.[STATE] as 'State',
cu.[ZIPCODE] as 'ZipCode',
[Sales Engineer] as 'Sales Engineer',
cu.[SalesGroup] as 'SalesGroup',
IsNull(tes.SALESMANID,'') as 'SALESMANID',
SUM([YTD Sales]) as 'YTD Sales',
SUM([1]) as '1',
SUM([2]) as '2',
SUM([3]) as '3',
SUM([4]) as '4',
SUM([5]) as '5',
SUM([6]) as '6',
SUM([7]) as '7',
SUM([8]) as '8',
SUM([9]) as '9',
SUM([10]) as '10',
SUM([11]) as '11',
SUM([12]) as '12',
SUM([Last YTD Sales]) as 'Last YTD Sales',
SUM([Last Year Sales]) as 'Last Year Sales',
SUM([L1]) as 'L1',
SUM([L2]) as 'L2',
SUM([L3]) as 'L3',
SUM([L4]) as 'L4',
SUM([L5]) as 'L5',
SUM([L6]) as 'L6',
SUM([L7]) as 'L7',
SUM([L8]) as 'L8',
SUM([L9]) as 'L9',
SUM([L10]) as 'L10',
SUM([L11]) as 'L11',
SUM([L12]) as 'L12',
IsNULL(Projection,0.00) as 'Projection',
[YTD Projection] as 'YTD Projection',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #1YTDStart
and ENDDATE <= #1YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL( ISNULL( NULLIF(d2.NUM,null), d1.NUM),null )
and SalesUnitId = cu.[SalesGroup] ), 0) AS 'YTD1',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE> = #2YTDStart
and ENDDATE <= #2YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL( ISNULL( NULLIF(d2.NUM,null), d1.NUM), null )
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD2',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #3YTDStart
and ENDDATE <= #3YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL( ISNULL( NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD3',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #4YTDStart
and ENDDATE <= #4YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL( ISNULL( NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD4',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #5YTDStart
and ENDDATE <= #5YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD5',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #6YTDStart
and ENDDATE <= #6YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD6',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #7YTDStart
and ENDDATE <= #7YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD7',
ISNULL( ( Select Top(1)[YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #8YTDStart
and ENDDATE <= #8YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD8',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #9YTDStart
and ENDDATE <= #9YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD9',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #10YTDStart
and ENDDATE <= #10YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD10',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #11YTDStart
and ENDDATE <= #11YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD11',
ISNULL( ( Select Top(1) [YTD Projection]
from #tempSalesTargetMW
where STARTDATE >= #12YTDStart
and ENDDATE <= #12YTDEnd
and CUSTACCOUNT = cu.[ACCOUNTNUM]
and Division = IsNULL(ISNULL(NULLIF(d2.NUM,null), d1.NUM),null)
and SalesUnitId = cu.[SalesGroup]),0) AS 'YTD12',
[Year Goal] as 'Year Goal',
[PROJECTION COMMENT],
0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
[CUSTTABLE] cu
Left JOIN ( select
LT.[DIMENSION2_] AS 'Division ID',
LJ.ACCOUNTNUM as 'ACCOUNTNUM',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1 * SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
LT.[TRANSDATE],
0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
dbo.[LedgerTrans] LT
inner JOIN dbo.[LedgerJournalTrans] LJ
on LT.Voucher = LJ.Voucher
and LT.TransDate = LJ.TransDate
and AccountType in ('1')
Inner Join dbo.LedgerTable LD
on LT.ACCOUNTNUM = LD.ACCOUNTNUM
where
LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] = '10'
and LT.Voucher Not Like 'JE%'
and LD.KTI_OASALESCOMM = 0
AND LT.[DIMENSION2_] in (#dimension)
and #reportType='Forecasting'
or
( LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] = '10'
and LT.Voucher Not Like 'JE%'
AND LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension)
and #reportType='Financial')
group by
LJ.ACCOUNTNUM,
LT.[DIMENSION2_],
LT.[TRANSDATE]
union all
select
LT.[DIMENSION2_] AS 'Division ID',
LJ.ACCOUNTNUM as 'ACCOUNTNUM',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #YTD THEN LT.[AMOUNTMST] ELSE 0 END) AS 'YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #1Start AND #1End THEN LT.[AMOUNTMST] ELSE 0 END) AS '1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #2Start AND #2End THEN LT.[AMOUNTMST] ELSE 0 END) AS '2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #3Start AND #3End THEN LT.[AMOUNTMST] ELSE 0 END) AS '3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #4Start AND #4End THEN LT.[AMOUNTMST] ELSE 0 END) AS '4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #5Start AND #5End THEN LT.[AMOUNTMST] ELSE 0 END) AS '5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #6Start AND #6End THEN LT.[AMOUNTMST] ELSE 0 END) AS '6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #7Start AND #7End THEN LT.[AMOUNTMST] ELSE 0 END) AS '7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #8Start AND #8End THEN LT.[AMOUNTMST] ELSE 0 END) AS '8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #9Start AND #9End THEN LT.[AMOUNTMST] ELSE 0 END) AS '9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #10Start AND #10End THEN LT.[AMOUNTMST] ELSE 0 END) AS '10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #11Start AND #11End THEN LT.[AMOUNTMST] ELSE 0 END) AS '11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #12Start AND #12End THEN LT.[AMOUNTMST] ELSE 0 END) AS '12',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYTD_Start AND #LYTD_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last YTD Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #LYS_Start AND #LYS_End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'Last Year Sales',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L1Start AND #L1End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L1',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L2Start AND #L2End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L2',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L3Start AND #L3End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L3',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L4Start AND #L4End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L4',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L5Start AND #L5End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L5',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L6Start AND #L6End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L6',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L7Start AND #L7End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L7',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L8Start AND #L8End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L8',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L9Start AND #L9End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L9',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L10Start AND #L10End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L10',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L11Start AND #L11End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L11',
-1*SUM(CASE WHEN LT.[TRANSDATE] BETWEEN #L12Start AND #L12End THEN LT.[AMOUNTMST] ELSE 0 END) AS 'L12',
LT.[TRANSDATE],
0.00 AS 'Backlog',
0.00 AS 'Completed Not Invoiced'
from
dbo.[LedgerTrans] LT
inner JOIN dbo.[LedgerJournalTrans] LJ
on LT.Voucher = LJ.Voucher
and LT.TransDate = LJ.TransDate
and AccountType in ('1')
Inner Join dbo.LedgerTable LD
on LT.ACCOUNTNUM = LD.ACCOUNTNUM
where
LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] != '10'
and LD.KTI_OASALESCOMM = 0
and #reportType = 'Forecasting'
AND LT.[DIMENSION2_] in (#dimension)
OR
( LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] != '10'
and #reportType = 'Financial'
AND LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension) )
group by
LJ.ACCOUNTNUM,
LT.[DIMENSION2_],
LT.[TRANSDATE] ) as Cus
on cu.ACCOUNTNUM = Cus.ACCOUNTNUM
Inner JOIN dbo.DIMENSIONS d1
ON cu.[dimension2_] = NUM
LEFT OUTER JOIN dbo.DIMENSIONS
d2 ON Cus.[Division ID] = d2.NUM
Left JOIN #tempSalesTarget te
on cu.ACCOUNTNUM = te.CUSTACCOUNT
and te.Division = Cus.[Division ID]
and cu.SALESGROUP = te.SALESUNITID
left JOIN #tempSalesEngineers tes
ON cu.SALESGROUP = tes.SALESUNITID
and (te.SALESMANID = tes.SALESMANID or te.SalesManID IS NULL)
LEFT OUTER JOIN #tempCustIndustry
ON cu.ACCOUNTNUM = #tempCustIndustry.ACCOUNTNUM
where
IsNull(tes.SALESMANID,'') IN (#SalesEngineer)
AND cu.[dimension2_] in (#dimension)
GROUP BY
d1.[DESCRIPTION],
d2.[DESCRIPTION],
d1.NUM,
d2.NUM,
cu.[NAME],
cu.[ACCOUNTNUM],
BUSINESSSECTORID,
cu.[STREET],
cu.[CITY],
cu.[STATE],
cu.[ZIPCODE],
cu.[SalesGroup],
[Sales Engineer],
tes.SALESMANID,
[Projection],
[Year Goal],
[YTD Projection],
[PROJECTION COMMENT],
backlog
Now, in the lower queries where you are doing the "cu" and "cus" select/unions, you have some where clauses like...
where
( LT.ACCOUNTNUM between '400100' and '409400')
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] = '10'
and LT.Voucher Not Like 'JE%'
and LD.KTI_OASALESCOMM = 0
AND LT.[DIMENSION2_] in (#dimension)
and #reportType='Forecasting'
or
( LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] = '10'
and LT.Voucher Not Like 'JE%'
AND LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[DIMENSION2_] in (#dimension)
and #reportType='Financial' )
Is that what you expected (for both instances), or should it have been
where
( first set of criteria )
OR
( second set of criteria )
But could be simplified down to...
where
LT.ACCOUNTNUM between '400100' and '409400'
AND LT.[TRANSDATE] BETWEEN #LYS_Start AND #12End
and LT.[DIMENSION2_] = '10'
and LT.Voucher Not Like 'JE%'
AND LT.[DIMENSION2_] in (#dimension)
and ( ( LD.KTI_OASALESCOMM = 0
and #reportType='Forecasting' )
or
#reportType='Financial'
)

Pivoting on a field?

I use IBM DB2 SQL.
Here is my current query:
select
EXSHPE as "Shape",
EXDLVY as "Delivery",
Sum(Case When EXSURF = 'Print' Then EXRLTO Else 0 End) As Retail_Print,
Sum(Case When EXSURF = 'Pattern' Then EXRLTO Else 0 End) as Retail_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXRLTO Else 0 End) As Retail_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXRLTO Else 0 End) as Retail_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXRLTO Else 0 End) As Retail_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXRLTO Else 0 End) as Retail_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXWHLO Else 0 End) As Wholesale_Print,
Sum(Case When EXSURF = 'Pattern' Then EXWHLO Else 0 End) as Wholesale_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXWHLO Else 0 End) As Wholesale_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXWHLO Else 0 End) as Wholesale_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXWHLO Else 0 End) As Wholesale_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXWHLO Else 0 End) as Wholesale_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXUNTO Else 0 End) As Units_Print,
Sum(Case When EXSURF = 'Pattern' Then EXUNTO Else 0 End) as Units_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXUNTO Else 0 End) As Units_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXUNTO Else 0 End) as Units_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXUNTO Else 0 End) As Units_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXUNTO Else 0 End) as Units_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXAURA Else 0 End) As Actual_AUR_Print,
Sum(Case When EXSURF = 'Pattern' Then EXAURA Else 0 End) as Actual_AUR_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXAURA Else 0 End) As Actual_AUR_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXAURA Else 0 End) as Actual_AUR_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXAURA Else 0 End) As Actual_AUR_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXAURA Else 0 End) as Actual_AUR_UnknownC,
Sum(Case When EXSURF = 'Print' Then EXMERA Else 0 End) As Merch_AUR_Print,
Sum(Case When EXSURF = 'Pattern' Then EXMERA Else 0 End) as Merch_AUR_Pattern,
Sum(Case When EXSURF = 'Solid' Then EXMERA Else 0 End) As Merch_AUR_Solid,
Sum(Case When EXSURF = 'UnknownA' Then EXMERA Else 0 End) as Merch_AUR_UnknownA,
Sum(Case When EXSURF = 'UnknownB' Then EXMERA Else 0 End) As Merch_AUR_UnknownB,
Sum(Case When EXSURF = 'UnknownC' Then EXMERA Else 0 End) as Merch_AUR_UnknownC
from EXOWMSPD
Where (EXCO || '/' || EXDIV) = ?
Group By
EXSHPE,
EXDLVY
Order By
EXSHPE DESC,
EXDLVY DESC
Database looks like this:
I need to pivot of Surface, but dynamically.
The issue is, I only know the first 3 surface descriptions. But I need to be ready for up to 6.
Is there a way I could pivot this dynamically to grab the first 6 surfaces.
Example,
RETAIL_DOLLARS_1 10.00
RETAIL_DOLLARS_2 20.00
RETAIL_DOLLARS_3 50.00
RETAIL_DOLLARS_4 0.00
RETAIL_DOLLARS_5 0.00
RETAIL_DOLLARS_6 0.00
In this example, data was found for 3 surfaces, the other 3 I want filled with 0s.
I'm using this to make a report and I'll hide columns which will have SURFACE_DESCRIPTION_X equal to "".
Is there a way to do this?
Thanks
If you would like to cube the data on the various dimensions, try the
GROUP BY CUBE
clause