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'
)