2014-11-05 106 views
0

我有下面的查询,我需要进行更改,我正在努力使用where子句。TSQL条件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 

回答

0

OK,貌似下面是一个可行的解决方案,但我不知道它的理想解决方案。

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