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

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.

Related

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;

SQL Query conditional query with acumulated

I have the following stored procedure:
select
employeename,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 1 THEN cs.amount ELSE 0 END), 0) as january,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 2 THEN cs.amount ELSE 0 END), 0) as february,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 3 THEN cs.amount ELSE 0 END), 0) as march,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 4 THEN cs.amount ELSE 0 END), 0) as april,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 5 THEN cs.amount ELSE 0 END), 0) as may,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 6 THEN cs.amount ELSE 0 END), 0) as june,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 7 THEN cs.amount ELSE 0 END), 0) as july,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 8 THEN cs.amount ELSE 0 END), 0) as august,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 9 THEN cs.amount ELSE 0 END), 0) as september,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 10 THEN cs.amount ELSE 0 END), 0) as october,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 11 THEN cs.amount ELSE 0 END), 0) as november,
ISNULL(SUM(CASE WHEN datepart(mm,cs.scheduledate) = 12 THEN cs.amount ELSE 0 END), 0) as december,
ISNULL(SUM(cs.amount),0) as a_cds
from
statements cs
join
employee mc on mc.employeeid = cs.employeeid
where
cs.chargetype = #chargetype
-- if #accumulate = 1 then SUM all statement amounts from old months
and ((datepart(mm, cs.scheduledate) <= #month and #accumulate = 1)
or (datepart(mm, cs.scheduledate) = #month and #accumulate = 0))
-- if selection is Biweekly (Q) then filter by that
and ((#chargetype = 'Q' and #biweeklypart = 1 and day(cs.scheduledate) = 15) or
(#chargetype = 'Q' and #biweeklypart = 2 and day(cs.scheduledate) <> 15) or
(#chargetype <> 'Q')
)
group by
employeename
Everything works great with this SP but the Accumulate filter. It only works when #chargetype is not 'Q'.
So let's look at the following sample data:
For Employee 15:
#accumulate = 1
#month = 2
#chargetype = 'Q'
#biweeklypart = 2
I should get the SUM of all previous statement records including 02/28/2018. As said this works fine when #chargetype = 'M'.
Any clue?

Need Multiple Values from Single Column

I am trying to join these two tables, or use a sub-query to achieve the results I need.
As of now I have the first query pulling hourly totals with a overall total between MIN(time) and MAX(time). I need to join the second query to get the total scans from MIN(time) and MAX(time)
Any ideas of where I could be going wrong?
select
UPPER(t.operator) as Operator,
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END)) ELSE NULL END AS '6AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END)) ELSE NULL END AS '7AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END)) ELSE NULL END AS '8AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END)) ELSE NULL END AS '9AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END)) ELSE NULL END AS '10AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END)) ELSE NULL END AS '11AM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END)) ELSE NULL END AS '12PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END)) ELSE NULL END AS '1PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END)) ELSE NULL END AS '2PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END)) ELSE NULL END AS '3PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END)) ELSE NULL END AS '4PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END)) ELSE NULL END AS '5PM',
CASE WHEN t.eventtype = 'Replenishment Complete' THEN (COUNT(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN t.eventtype ELSE NULL END)) ELSE NULL END AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
CAST(t.time as DATE) = CAST(GETDATE() as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator,
t.eventtype
Second query:
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
CAST(o.time as DATE) = CAST(GETDATE() as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
order by o.operator
Something like the below should get you headed to a solution. I removed the group on t.eventtype and removed from the case statements because the where clause already limited the results for you.
select
op.Operator,
op.TotalScans,
op.LogonTime,
op.TotalMinLogon,
b.[6AM],
b.[7AM],
b.[8AM],
b.[9AM],
b.[10AM],
b.[11AM],
b.[12PM],
b.[1PM],
b.[2PM],
b.[3PM],
b.[4PM],
b.[5PM],
b.[6+PM]
from
(
select
UPPER(o.operator) as Operator,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1 ELSE 0 END) as TotalScans,
convert( varchar(19), MIN(o.time), 8) as LogonTime,
DATEDIFF(MINUTE, MIN(o.time), MAX(o.time)) as TotalMinLogon
from mck_hvs.replevent o
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and o.eventtype IN ( 'Replenishment Complete', 'GoodScan' )
group by
o.operator
) as op
left join
(
select
UPPER(t.operator) as Operator,
SUM(CASE WHEN DATEPART(HOUR,t.time) = 6 THEN 1 ELSE 0 END) AS '6AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 7 THEN 1 ELSE 0 END) AS '7AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 8 THEN 1 ELSE 0 END) AS '8AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 9 THEN 1 ELSE 0 END) AS '9AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 10 THEN 1 ELSE 0 END) AS '10AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 11 THEN 1 ELSE 0 END) AS '11AM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 12 THEN 1 ELSE 0 END) AS '12PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 13 THEN 1 ELSE 0 END) AS '1PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 14 THEN 1 ELSE 0 END) AS '2PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 15 THEN 1 ELSE 0 END) AS '3PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 16 THEN 1 ELSE 0 END) AS '4PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) = 17 THEN 1 ELSE 0 END) AS '5PM',
SUM(CASE WHEN DATEPART(HOUR,t.time) > 17 THEN 1 ELSE 0 END) AS '6+PM',
SUM(CASE WHEN t.eventtype = 'Replenishment Complete' THEN 1 ELSE 0 END) as TotalCanisters
from mck_hvs.replevent t
where
--will use o.time index now:
o.time >= CAST(GETDATE() as DATE)
and o.time < CAST(dateadd(Day,1,GETDATE()) as DATE)
and t.stationtype IN ('T', 'S')
and t.eventtype = 'Replenishment Complete'
group by
t.operator
) as b
on b.Operator = op.Operator
SELECT l.* ,
r.TotalScans ,
r.LogonTime ,
r.TotalMinLogon
FROM ( SELECT UPPER(t.operator) AS Operator ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 6
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '6AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 7
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '7AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 8
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '8AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 9
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '9AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 10
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '10AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 11
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '11AM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 12
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '12PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 13
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '1PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 14
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '2PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 15
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '3PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 16
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '4PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( SUM(CASE WHEN DATEPART(HOUR, t.time) = 17
THEN 1
ELSE 0
END) )
ELSE NULL
END AS '5PM' ,
CASE WHEN t.eventtype = 'Replenishment Complete'
THEN ( COUNT(CASE WHEN DATEPART(HOUR, t.time) > 17
THEN t.eventtype
ELSE NULL
END) )
ELSE NULL
END AS '6+PM' ,
SUM(CASE WHEN t.eventtype = 'Replenishment Complete'
THEN 1
ELSE 0
END) AS TotalCanisters
FROM #replevent t
WHERE CAST(t.time AS DATE) = CAST(GETDATE() AS DATE)
AND t.stationtype IN ( 'T', 'S' )
AND t.eventtype = 'Replenishment Complete'
GROUP BY t.operator ,
t.eventtype
) AS l
INNER JOIN ( SELECT UPPER(o.operator) AS Operator_r ,
SUM(CASE WHEN o.eventtype = 'GoodScan' THEN 1
ELSE 0
END) AS TotalScans ,
CONVERT(VARCHAR(19), MIN(o.time), 8) AS LogonTime ,
DATEDIFF(SECOND, MIN(o.time), MAX(o.time)) AS TotalMinLogon
FROM #replevent o
WHERE CAST(o.time AS DATE) = CAST(GETDATE() AS DATE)
AND o.eventtype IN ( 'Replenishment Complete',
'GoodScan' )
GROUP BY o.operator
) AS r ON l.Operator = r.Operator_r
ORDER BY l.Operator;

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=,

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.

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