Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server WHERE子句使用临时列_Sql_Sql Server_Tsql_Calculated Columns - Fatal编程技术网

SQL Server WHERE子句使用临时列

SQL Server WHERE子句使用临时列,sql,sql-server,tsql,calculated-columns,Sql,Sql Server,Tsql,Calculated Columns,我有以下查询,它使用CASE语句。 IsBusinessDayFinal=0的where子句中是否仍有需要添加的内容?不使用临时桌子 非常感谢 SELECT ac.DateTimeValue, CASE WHEN pc.IsBusinessDay IS NOT NULL THEN pc.IsBusinessDay ELSE ac.IsBusinessDay END AS IsBusinessDay

我有以下查询,它使用CASE语句。 IsBusinessDayFinal=0的where子句中是否仍有需要添加的内容?不使用临时桌子

非常感谢

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;
选择
ac.DateTimeValue,
案例
当pc.IsBusinessDay不为空时,则pc.IsBusinessDay
ELSE ac.IsBusinessDay
以IsBusinessDayFinal结束,
ac.全年,
ac.MonthValue,
ac.DayOfMonth,
ac.DayOfWeek,
交流周
从…起
[dbo].[AdminCalendar]ac左加入
[dbo].ac.DateTimeValue=pc.DateTimeValue和pc.ProjectId=@ProjectId上的ProjectCalendar pc
其中ac.DateTimeValue>=@startDate和ac.DateTimeValue
其中ac.DateTimeValue>=@startDate和ac.DateTimeValue使用

您可以使用而不是大小写,因为您正在检查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
选择ac.DateTimeValue,
合并(pc.IsBusinessDay,ac.IsBusinessDay)为IsBusinessDay最终版本,
ac.全年,
ac.MonthValue,
ac.DayOfMonth,
ac.DayOfWeek,
交流周
从[dbo].[AdminCalendar]ac
左连接[dbo]。ac.DateTimeValue=pc.DateTimeValue上的ProjectCalendar pc
和pc.ProjectId=@ProjectId
其中ac.DateTimeValue>=@startDate

和ac.DateTimeValue您能使用子选择吗?谢谢!我没有意识到解决方案其实很简单。我想我想得不够仔细=p+1。即使逻辑是相同的,我还是喜欢你的是空版本!
WHERE (pc.IsBusinessDay IS NULL AND ac.IsBusinessDay = 0)
OR pc.IsBusinessDay = 0
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