I am trying to get a count of activities by month and group them by a department name. The report is based on a date parameter that I input to the query "#rptDate". What I have below works, but I'm guessing there is a better way of doing the same thing and hoping someone can shed some light.
Select Count(*) As Month1Count, 0 As Month2Count, 0 As Month3Count,
0 As Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) and Year(BeginDate) = Year(#rptDate)
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, COUNT(*) As Month2Count, 0 as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName,ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 1 and Year(BeginDate) = Year(DateAdd(month,1,#rptDate))
Group By DepartmentName, ActivityDescription
UNION
Select 0 As Month1Count, 0 As Month2Count, Count(*) as Month3Count,
0 as Month4Count, 0 as Month5Count, 0 as Month6Count, 0 as Month7Count,
0 as Month8Count, 0 as Month9COunt, 0 as Month10Count, 0 as Month11Count,
0 as Month12Count, DepartmentName, ActivityDescription
from reports.WorkOrders
WHERE Month(BeginDate) = Month(#rptDate) + 2 and Year(BeginDate) = Year(DateAdd(month,2,#rptDate))
Group By DepartmentName, ActivityDescription
I haven't tested the following, so it's possible there are some typos or minor errors, but I think the following is what you want:
SELECT
sum(case when Month(BeginDate) = Month(dateadd(month, 0, #rptDate)) then 1 else 0 end) as Month1Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 1, #rptDate)) then 1 else 0 end) as Month2Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 2, #rptDate)) then 1 else 0 end) as Month3Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 3, #rptDate)) then 1 else 0 end) as Month4Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 4, #rptDate)) then 1 else 0 end) as Month5Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 5, #rptDate)) then 1 else 0 end) as Month6Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 6, #rptDate)) then 1 else 0 end) as Month7Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 7, #rptDate)) then 1 else 0 end) as Month8Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 8, #rptDate)) then 1 else 0 end) as Month9Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 9, #rptDate)) then 1 else 0 end) as Month10Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 10, #rptDate)) then 1 else 0 end) as Month11Count,
sum(case when Month(BeginDate) = Month(dateadd(month, 11, #rptDate)) then 1 else 0 end) as Month12Count
DepartmentName,
ActivityDescription
FROM
reports.WorkOrders
WHERE
Year(BeginDate) + right('0'+Month(BeginDate),2)
between Year(#rptDate) + right('0'+Month(#rptDate),2)
and Year(dateadd(month,11,#rptDate)) + right('0'+Month(dateadd(month,11,#rptDate)),2)
GROUP BY
DepartmentName,
ActivityDescription
Do let me know how you get on with this. If I've made typos and you can't correct for them, do report the error message and/or any problems with the results.
I am trying to store the following sql in a bean within an XML file. I am trying to escape the greater than and less than signs, however I seem to still be getting an error in Eclipse. I must not understand how to properly escape these signs. I count six less than and greater than signs, which I have attempted to escape.
Here's my attempt:
<property name="SQL" value="<><><><><><>"
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
,'STG_DISB_HOLD_TH' SRC_TBL
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
,COUNT(1) ROW_CNT
,SUM(A1.APPL_NBR) APPL_NBR_SUM
,SUM(A1.TSYS_SCHL_ID) TSYS_SCHL_ID_SUM
,SUM(TO_NUMBER(A1.DISB_NBR)) DISB_NBR_SUM
,SUM(A1.DISB_SEQ_NBR) DISB_SEQ_NBR_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 THEN 1
ELSE A1.INTNL_APPL_SEQ_NBR + 1
END) INTERNAL_DISB_SEQ_SUM
,MIN(A1.DISB_RCVD_DT) DISB_RCVD_DT_MIN
,MAX(A1.DISB_RCVD_DT) DISB_RCVD_DT_MAX
,SUM(CASE WHEN A1.DISB_STAT_CD = 'P' THEN 1 ELSE 0 END) DISB_STAT_CD_P
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' THEN 1 ELSE 0 END) DISB_STAT_CD_R
,MIN(A1.PYMT_START_DT) PYMT_START_DT_MIN
,MAX(A1.PYMT_START_DT) PYMT_START_DT_MAX
,COUNT(DISTINCT A1.SCHL_ENROLL_CD) SCHL_ENROLL_CD_CNT
,SUM(TO_NUMBER(A1.NET_TRANS_CD)) NET_TRANS_CD_SUM
,SUM(A1.NET_TRANS_AMT) NET_TRANS_AMT_SUM
,SUM(CASE WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND = 'D' THEN A1.NET_TRANS_AMT + A1.NET_TRANS_AMT
WHEN A1.DISB_STAT_CD = 'R' AND A1.DISB_SEQ_NBR = 1 AND A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND = 'D' THEN A1.SUM_NET_AMT + A1.NET_TRANS_AMT
WHEN (A1.DISB_STAT_CD <> 'R' OR A1.DISB_SEQ_NBR <> 1) AND A1.NET_C_OR_D_IND <> 'D' THEN A1.SUM_NET_AMT - A1.NET_TRANS_AMT
ELSE 0
END) SUM_NET_AMT_SUM
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'F' THEN 1 ELSE 0 END) ENRL_STATUS_CD_F
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'Q' THEN 1 ELSE 0 END) ENRL_STATUS_CD_Q
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'H' THEN 1 ELSE 0 END) ENRL_STATUS_CD_H
,SUM(CASE WHEN A1.ENRL_STATUS_CD = 'L' THEN 1 ELSE 0 END) ENRL_STATUS_CD_L
,SUM(CASE WHEN A1.ENRL_STATUS_CD IS NULL THEN 1 ELSE 0 END) ENRL_STATUS_CD_NULL
,COUNT(DISTINCT A1.PGM_CIP_CD) PGM_CIP_CD_CNT
,COUNT(DISTINCT A1.LEGACY_USER_ID) LEGACY_USER_ID_CNT
,COUNT(DISTINCT A1.MAINT_APP) MAINT_APP_CNT
,MIN(A1.MAINT_DTM) MAINT_DTM_MIN
,MAX(A1.MAINT_DTM) MAINT_DTM_MAX
,COUNT(DISTINCT A1.MAINT_USERID) MAINT_USERID_CNT
FROM
(
SELECT LM.TEACH_MASTER_ID
,SAM.APPL_NBR
,TO_NUMBER(SAM.AWARD_YR) AWARD_YR
,SAM.TSYS_SCHL_ID
,SAM.AWARD_NBR
,SAM.DISB_NBR
,SAM.DISB_SEQ_NBR
,SAM.CPC
,SAM.DISB_DT
,SAM.DISB_RCVD_DT
,SAM.DISB_STAT_CD
,SAM.PYMT_START_DT
,SAM.SCHL_ENROLL_CD
,SAM.NET_TRANS_CD
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.NET_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.NET_TRANS_AMT
END NET_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.GROSS_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.GROSS_TRANS_AMT
END GROSS_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.FEE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.FEE_TRANS_AMT
END FEE_TRANS_AMT
,CASE WHEN SAM.NET_C_OR_D_IND = 'C' THEN SAM.REBATE_TRANS_AMT * -1
WHEN SAM.NET_C_OR_D_IND IS NULL THEN 0
ELSE SAM.REBATE_TRANS_AMT
END REBATE_TRANS_AMT
,SAM.MAINT_DTM
,SAM.MAINT_USERID
,SAM.ENRL_STATUS_CD
,SAM.PGM_CIP_CD
,'BATCH' AS LEGACY_USER_ID
,'BATCH' AS MAINT_APP
,SAM.NET_C_OR_D_IND
,ROW_NUMBER() OVER (PARTITION BY SAM.APPL_NBR, SAM.AWARD_YR, SAM.TSYS_SCHL_ID, SAM.AWARD_NBR, SAM.DISB_NBR
ORDER BY DTSEQ.INTNL_APPL_SEQ_NBR DESC) ROW_NUM_PART
,COALESCE(DTSEQ.INTNL_APPL_SEQ_NBR,0) INTNL_APPL_SEQ_NBR
,DTSEQ.CHG_GROSS_TRANS_AMT DT_CHG_GROSS_TRANS_AMT
,DTSEQ.CHG_NET_TRANS_AMT DT_CHG_NET_TRANS_AMT
,DTSEQ.CHG_FEE_TRANS_AMT DT_CHG_FEE_TRANS_AMT
,DTSEQ.CHG_REBATE_TRANS_AMT DT_CHG_REBATE_TRANS_AMT
,COALESCE(DTSEQ.SUM_GROSS_AMT,0) SUM_GROSS_AMT
,COALESCE(DTSEQ.SUM_NET_AMT,0) SUM_NET_AMT
,COALESCE(DTSEQ.SUM_FEE_AMT,0) SUM_FEE_AMT
,COALESCE(DTSEQ.SUM_REBATE_AMT,0) SUM_REBATE_AMT
FROM MYSCHEMA.TEACH_MASTER LM
JOIN MYSCHEMA.STG_DISB_HOLD SAM
ON LM.APP_NUM_LEGACY = SAM.APPL_NBR
AND LM.ATTEND_SCHL_MASTER_ID = SAM.TSYS_SCHL_ID
AND LM.AWARD_YR = TO_NUMBER(SAM.AWARD_YR)
LEFT JOIN MYSCHEMA.STG_DISB_TRANS DTSEQ
ON SAM.APPL_NBR = DTSEQ.APPL_NBR
AND SAM.AWARD_YR = DTSEQ.AWARD_YR
AND SAM.TSYS_SCHL_ID = DTSEQ.TSYS_SCHL_ID
AND SAM.AWARD_NBR = DTSEQ.AWARD_NBR
AND SAM.DISB_NBR = DTSEQ.DISB_NBR
WHERE SAM.DISB_STAT_CD IN ('R','P')
AND SAM.NET_TRANS_CD IN ('0102','0131','0161','0189','0200')
) A1
WHERE A1.ROW_NUM_PART = 1
GROUP BY TO_CHAR(SYSDATE,'YYYY-MM-DD')
,'STG_DISB_HOLD_TH'
,A1.DISB_DT
,A1.CPC
,A1.AWARD_YR
;"/>
You have to escape them ALL:
WHEN A1.DISB_STAT_CD [..snip..] A1.NET_C_OR_D_IND <> 'D' THEN A1.NET_TRANS_AMT - A1.NET_TRANS_AMT
^^--missed a bunch of these
Plus, this next bit makes no sense:
<property name="SQL" value="<><><><><><>"
^---start attribute end attribute --^
"SELECT TO_CHAR(SYSDATE,'YYYY-MM-DD') SQL_RUN_DT
^---attribute value with no name for it
Got this error and facing a bit because the alias but my head just turning around with stuffs, could you help me to solve the alias wrong check this
SELECT B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC, ADJ_FG_QTY_PREV = SUM(B.ADJ_FG_QTY_PREV), ADJ_COGS_QTY_PREV = SUM(B.ADJ_COGS_QTYPREV) , ADJ_FG_QTY = SUM(B.ADJ_FG_QTY), ADJ_COGS_QTY = SUM(B.ADJ_COGS_QTY)
FROM
(
SELECT
PARTS_NO=a.PARTS_NO,
Adj_Date=Convert(varchar(10),a.PROCESSING_DATETIME,105),
COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)
ELSE 0
END
FROM TITEMBALANCE a
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))
AND a.PARTS_NO=a.PARTS_NO),0),
ADJ_DESC ='CATS',
ADJ_FG_QTY_PREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)
FROM TPARTADJUSTMENTC a INNER JOIN TPARTADJUSTMENTCL b ON a.CARD_NO=b.CARD_NO AND a.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND a.PARTS_NO=b.PARTS_NO AND a.PROCESS_CODE=b.PROCESS_CODE AND a.FILLER=b.FILLER AND a.PROCESSING_DATETIME=b.PROCESSING_DATETIME
WHERE ISNULL(a.COSTING1,0)=Convert(numeric(6,0),#Period)
GROUP BY a.PARTS_NO,Convert(varchar(10),a.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)
UNION ALL
SELECT
PARTS_NO=a.PARTS_NO,
Adj_Date=Convert(varchar(10),a.PROCESSING_DATETIME,105),
COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)
ELSE 0
END
FROM TITEMBALANCE a
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))
AND a.PARTS_NO=a.PARTS_NO),0),
ADJ_DESC ='IMPULSE',
ADJ_FG_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)
FROM TPARTADJUSTMENTI a INNER JOIN TPARTADJUSTMENTIL b ON a.CARD_NO=b.CARD_NO AND a.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND a.PARTS_NO=b.PARTS_NO AND a.PROCESS_CODE=b.PROCESS_CODE AND a.FILLER=b.FILLER AND a.PROCESSING_DATETIME=b.PROCESSING_DATETIME
WHERE ISNULL(a.COSTING1,0)=Convert(numeric(6,0),#Period)
GROUP BY a.PARTS_NO,Convert(varchar(10),a.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)
) B GROUP BY B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC
You are using the alias a for both TITEMBALANCE in the subquery and TPARTADJUSTMENTC in the query.
I think that the condition a.PARTS_NO=a.PARTS_NO is supposed to compare fields from both tables, but because of the conflicting aliases it will just compare a field to itself and the query will return all parts. That will make the subquery return more than one record, and you get that error message.
This might help the answer, I changed 2 name alias between 2 union. Thanks everyone
SELECT #ProsesPeriode as TanggalPeriod , B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC, ADJ_FG_QTY_PREV = SUM(B.ADJ_FG_QTY_PREV), ADJ_COGS_QTY_PREV = SUM(B.ADJ_COGS_QTYPREV) , ADJ_FG_QTY = SUM(B.ADJ_FG_QTY), ADJ_COGS_QTY = SUM(B.ADJ_COGS_QTY)
FROM
(
SELECT
PARTS_NO=d.PARTS_NO,
Adj_Date=Convert(varchar(10),d.PROCESSING_DATETIME,105),
COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)
ELSE 0
END
FROM TITEMBALANCE a
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))
AND a.PARTS_NO=d.PARTS_NO),0),
ADJ_DESC ='CATS',
ADJ_FG_QTY_PREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))<>ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))<>ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))=ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))=ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)
FROM TPARTADJUSTMENTC d INNER JOIN TPARTADJUSTMENTCL b ON d.CARD_NO=b.CARD_NO AND d.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND d.PARTS_NO=b.PARTS_NO AND d.PROCESS_CODE=b.PROCESS_CODE AND d.FILLER=b.FILLER AND d.PROCESSING_DATETIME=b.PROCESSING_DATETIME
WHERE ISNULL(d.COSTING1,0)=Convert(numeric(6,0),#Period)
GROUP BY d.PARTS_NO,Convert(varchar(10),d.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)
UNION ALL
SELECT
PARTS_NO=k.PARTS_NO,
Adj_Date=Convert(varchar(10),k.PROCESSING_DATETIME,105),
COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)
WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)
ELSE 0
END
FROM TITEMBALANCE a
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(#Period))+'01',112))
AND a.PARTS_NO=k.PARTS_NO),0),
ADJ_DESC ='IMPULSE',
ADJ_FG_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))<>ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))<>ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))=ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),
ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))=ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)
FROM TPARTADJUSTMENTI k INNER JOIN TPARTADJUSTMENTIL b ON k.CARD_NO=b.CARD_NO AND k.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND k.PARTS_NO=b.PARTS_NO AND k.PROCESS_CODE=b.PROCESS_CODE AND k.FILLER=b.FILLER AND k.PROCESSING_DATETIME=b.PROCESSING_DATETIME
WHERE ISNULL(k.COSTING1,0)=Convert(numeric(6,0),#Period)
GROUP BY k.PARTS_NO,Convert(varchar(10),k.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)
) B GROUP BY B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC
The root cause of your problem with this query is actually conflicting Aliases for Tables ItemBalance & TPartAdjustmentC.
The Same Alias (a) being used for the tables in the union All renders the condition comparing the part numbers as ambiguous.