TSQL条件where子句问题

时间:2014-11-05 11:52:51

标签: sql-server-2008 tsql where-clause

我有以下查询,我需要对其进行更改,并且我正在努力使用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

1 个答案:

答案 0 :(得分:0)

好的,看起来下面的解决方案有效,但我不确定它是否是理想的解决方案。

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