2

我有以下查询,它使用 CASE 语句。无论如何要添加到 where 子句 WHERE IsBusinessDayFinal = 0 中吗?不使用临时表?

非常感谢!

SELECT 
        ac.DateTimeValue,
        CASE 
            WHEN pc.IsBusinessDay IS NOT NULL THEN pc.IsBusinessDay
            ELSE ac.IsBusinessDay
        END AS IsBusinessDayFinal,
        ac.FullYear,
        ac.MonthValue,
        ac.DayOfMonth,
        ac.DayOfWeek,
        ac.Week 
    FROM 
        [dbo].[AdminCalendar] ac LEFT JOIN
        [dbo].ProjectCalendar pc ON ac.DateTimeValue = pc.DateTimeValue AND pc.ProjectId = @projectId
    WHERE ac.DateTimeValue >= @startDate AND ac.DateTimeValue <= @finishDate;
4

3 回答 3

3

采用

WHERE (pc.IsBusinessDay IS NULL AND ac.IsBusinessDay = 0)
OR pc.IsBusinessDay = 0
于 2009-12-12T02:43:31.733 回答
2
WHERE ac.DateTimeValue >= @startDate AND ac.DateTimeValue <= @finishDate
      AND ((pc.IsBusinessDay IS NOT NULL AND pc.IsBusinessDay = 0) OR ac.IsBusinessDay = 0)
于 2009-12-12T02:42:49.597 回答
2

您可以使用COALESCE而不是 CASE 因为您正在检查 null:

SELECT ac.DateTimeValue,
       COALESCE(pc.IsBusinessDay, ac.IsBusinessDay) AS IsBusinessDayFinal,
       ac.FullYear,
       ac.MonthValue,
       ac.DayOfMonth,
       ac.DayOfWeek,
       ac.Week 
  FROM [dbo].[AdminCalendar] ac 
LEFT JOIN [dbo].ProjectCalendar pc ON ac.DateTimeValue = pc.DateTimeValue 
                                  AND pc.ProjectId = @projectId
WHERE ac.DateTimeValue >= @startDate 
 AND ac.DateTimeValue <= @finishDate
 AND COALESCE(pc.IsBusinessDay, ac.IsBusinessDay) = 0
于 2009-12-12T02:52:30.877 回答