Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Case statment in Join clause!

Author  Topic 

moodi_z
Starting Member

38 Posts

Posted - 2009-01-28 : 06:33:13
Hi,

How can I use case statment in a join clause?! Example:

Select A.GroupField, A.GroupSum, mydateTime AS Date
From (
SELECT Case when @GroupBy='Work Center' then WorkCenterID
when @GroupBy='Equipment' then EquipmentID
when @GroupBy='WIP' then WipID
when @GroupBy='Product' then ProductID
when @GroupBy='Scrap Reason' then ReasonID
when @GroupBy='Off Time Reason' then ReasonID
when @GroupBy='Team' then UsersGroupID
End AS GroupField,
sum(Qty) AS GroupSum

FROM tblP_OEE_Production_Transactions

WHERE (FacilityID = @Facility OR @Facility = 'All') AND
(WorkCenterID = @WorkCenter OR @WorkCenter = 'All') AND
(EquipmentID = @Equipment OR @Equipment = 'All') AND
(WipID IN (select Extracted from dbo.MultiValuesTable(@WIPID, ','))) AND
(SubWipID IN (select Extracted from dbo.MultiValuesTable(@SubWIPID, ','))) AND
(ProductID IN (select Extracted from dbo.MultiValuesTable(@Product, ','))) AND
(UsersGroupID = @Team OR @Team = 'All') AND
(ReasonID In (select Extracted from dbo.MultiValuesTable(@Reason, ',')) OR @Reason='No Reason') AND
([Type] = @DataMeasure) AND
(mydatetime BETWEEN @FromDate AND @ToDate)


Group By Case when @GroupBy='Work Center' then WorkCenterID
when @GroupBy='Equipment' then EquipmentID
when @GroupBy='WIP' then WipID
when @GroupBy='Product' then ProductID
when @GroupBy='Scrap Reason' then ReasonID
when @GroupBy='Off Time Reason' then ReasonID
when @GroupBy='Team' then UsersGroupID
End ) AS A

LEFT OUTER JOIN tblP_OEE_Production_Transactions AS B ON
Case when @GroupBy='Work Center' then A.WorkCenterID = B.WorkCenterID
when @GroupBy='Equipment' then A.EquipmentID = B.EquipmentID
when @GroupBy='WIP' then A.WipID = B.WipID
when @GroupBy='Product' then A.ProductID = B.ProductID
when @GroupBy='Scrap Reason' then A.ReasonID = B.ReasonID
when @GroupBy='Off Time Reason' then A.ReasonID = B.ReasonID
when @GroupBy='Team' then A.UsersGroupID = B.UsersGroupID
End

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-28 : 07:39:22
Yes you can.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-28 : 07:45:02
But you have to change it like this
LEFT OUTER JOIN tblP_OEE_Production_Transactions AS B ON 
Case
when @GroupBy='Work Center' and A.GroupField = B.WorkCenterID then 1
when @GroupBy='Equipment' and A.GroupField = B.EquipmentID then 1
when @GroupBy='WIP' and A.GroupField = B.WipID then 1
when @GroupBy='Product' and A.GroupField = B.ProductID then 1
when @GroupBy='Scrap Reason' and A.GroupField = B.ReasonID then 1
when @GroupBy='Off Time Reason' and A.GroupField = B.ReasonID then 1
when @GroupBy='Team' and A.GroupField = B.UsersGroupID then 1
else 0
End = 1



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-28 : 07:47:36
Or this
SELECT		A.GroupField,
A.GroupSum,
b.myDateTime AS Date
FROM (
SELECT CASE @GroupBy
WHEN 'Work Center' then WorkCenterID
when 'Equipment' then EquipmentID
when 'WIP' then WipID
when 'Product' then ProductID
when 'Scrap Reason' then ReasonID
when 'Off Time Reason' then ReasonID
when 'Team' then UsersGroupID
End AS GroupField,
SUM(Qty) AS GroupSum
FROM tblP_OEE_Production_Transactions
WHERE (FacilityID = @Facility OR @Facility = 'All')
AND (WorkCenterID = @WorkCenter OR @WorkCenter = 'All')
AND (EquipmentID = @Equipment OR @Equipment = 'All')
AND (WipID IN (select Extracted from dbo.MultiValuesTable(@WIPID, ',')))
AND (SubWipID IN (select Extracted from dbo.MultiValuesTable(@SubWIPID, ',')))
AND (ProductID IN (select Extracted from dbo.MultiValuesTable(@Product, ',')))
AND (UsersGroupID = @Team OR @Team = 'All')
AND (ReasonID In (select Extracted from dbo.MultiValuesTable(@Reason, ',')) OR @Reason='No Reason')
AND ([Type] = @DataMeasure)
AND (mydatetime BETWEEN @FromDate AND @ToDate)
GROUP BY CASE @GroupBy
WHEN 'Work Center' then WorkCenterID
when 'Equipment' then EquipmentID
when 'WIP' then WipID
when 'Product' then ProductID
when 'Scrap Reason' then ReasonID
when 'Off Time Reason' then ReasonID
when 'Team' then UsersGroupID
End
) AS A
LEFT JOIN (
SELECT Case @GroupBy
when 'Work Center' then WorkCenterID
when 'Equipment' then EquipmentID
when 'WIP' then WipID
when 'Product' then ProductID
when 'Scrap Reason' then ReasonID
when 'Off Time Reason' then ReasonID
when 'Team' then UsersGroupID
End AS GroupField,
myDateTime
FROM tblP_OEE_Production_Transactions
) AS B ON b.GroupField = a.GroupField



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -