[英]TSQL conditional where clause issue
我有下面的查詢,我需要進行更改,而我很難用where子句來表示。
所需要的變化是關於參數@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
在維護現有邏輯的同時,如何稱呼where子句以捕獲新條件?
查詢:
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
好的,看起來下面是一個可行的解決方案,但是我不確定它是否是理想的解決方案。
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 (@EquipmentBreakdown = 0 OR (@EquipmentBreakdown = 1 AND @PropertyCasualty = 1) OR (LCRep.EB = @EquipmentBreakdown))
AND (@PropertyCasualty = 0 OR (@EquipmentBreakdown = 1 AND @PropertyCasualty = 1) OR (LCRep.PC = @PropertyCasualty))
GROUP BY Zone.ZoneID, Zone.ZoneName, Region.RegionCode, Region.RegionName, Branch.BranchID, BranchName, SR01.ServicingRep, LCRep.RepName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.