0
다음 쿼리를 사용하여 변경해야하며 where 절을 사용하는 데 어려움을 겪고 있습니다.TSQL 조건부 절 발행
매개 변수에 관한 변경이 필요합니다. @EquipmentBreakdown
& @PropertyCasualty
. 현재 예상대로 작동하지만 새로운 조건이 필요합니다.
현재 조건 :
@EquipmentBreakdown = 0 AND @PropertyCasualty = 0 Return all rows
@EquipmentBreakdown = 1 AND @PropertyCasualty = 0 Return all rows where LCRep.EB = 1
@EquipmentBreakdown = 0 AND @PropertyCasualty = 1 Return all rows where LCRep.PC = 1
새로운 조건 : 기존 논리를 유지하면서
@EquipmentBreakdown = 1 AND @PropertyCasualty = 1 Return all rows
가 어떻게 절은 새로운 조건을 캡처하려면 어디를 부르는합니까?
쿼리 다음 아래 작동하는 솔루션입니다하지만이 경우 이상적인 솔루션 확실하지 않다처럼
SELECT 'Month' AS [Period],
ISNULL(Zone.ZoneID,'') AS ZoneID,
ISNULL(Zone.ZoneName,'') AS ZoneName,
ISNULL(Region.RegionCode,'') AS RegionCode,
ISNULL(Region.RegionName,'') AS RegionName,
Branch.BranchID,
ISNULL(Branch.BranchName,'') AS BranchName,
SR01.ServicingRep,
ISNULL(LCRep.RepName,'') AS RepName,
SUM(ISNULL(RequestsOnSiteView.CountOfRequests,0)) AS RequestsOnSite,
SUM(ISNULL(HoursOnSiteView.SumHours,0)) AS HoursOnSite,
SUM(ISNULL(RequestsOffSiteView.CountOfRequests,0)) AS RequestsOffSite,
SUM(ISNULL(HoursOffSiteView.SumHours,0)) AS HoursOffSite,
SUM(ISNULL(RequestsNonReportView.CountOfRequests,0)) AS RequestsNonReport,
SUM(ISNULL(HoursNonReportView.SumHours,0)) AS HoursNonReport
FROM dbo.SR01
LEFT JOIN dbo.HoursOnSiteView ON HoursOnSiteView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.HoursOffSiteView ON HoursOffSiteView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.HoursNonReportView ON HoursNonReportView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.RequestsOnSiteView ON RequestsOnSiteView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.RequestsOffSiteView ON RequestsOffSiteView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.RequestsNonReportView ON RequestsNonReportView.ReportKey = SR01.ReportKey
LEFT JOIN dbo.LCRep ON SR01.ServicingRep = LCRep.RepID
LEFT JOIN dbo.Branch ON SR01.ServicingBranch = Branch.BranchID
LEFT JOIN dbo.Region ON Region.RegionCode = Branch.Region
LEFT JOIN dbo.Zone ON Zone.ZoneAbbrev = Region.Zone
WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
AND SR01.[Status]='X'
AND SR01.RequestType <> 'MN'
AND SR01.DateComplete BETWEEN DATEADD(month, DATEDIFF(month, 0, @DateTo), 0) AND @DateTo
AND Zone.ZoneID IN (@Zone)
AND Region.RegionCode IN (@Region)
AND Branch.BranchID IN (@Branch)
AND LCRep.RepID IN(@RepID)
AND (@EquipmentBreakdown = 0 OR (LCRep.EB = @EquipmentBreakdown))
AND (@PropertyCasualty = 0 OR (LCRep.PC = @PropertyCasualty))
GROUP BY Zone.ZoneID, Zone.ZoneName, Region.RegionCode, Region.RegionName, Branch.BranchID, BranchName, SR01.ServicingRep, LCRep.RepName