我正在尝试创建一个where子句,它有两个一起工作的语句,然后是一个单独的语句,如果它符合某个条件,它就会自行工作
我已经尝试了CASE语句,AND',OR,但仍然无效
WHERE HB.CreatedByDate = @ReportDateN AND HB.Date = @ReportDateN - 1
所以这段代码是我当前的where子句,它得到的结果是CreatedByDate等于变量@ReportDateN,但是常规日期等于变量@ReportDateN,除了它减去一天。
这本身就是我想要它做的,但我想补充一些。
我想这样做,如果一个名为@WeekDay ='Monday'的变量然后在第二部分中从@ReportDateN中减去3而不是仅仅1。
任何帮助将不胜感激,谢谢!
WHERE HB.CreatedByDate = @ReportDateN
AND HB.DATE = CASE
WHEN @Weekday = 'Monday'
THEN (@ReportDateN - 3)
ELSE (@ReportDateN - 1)
END
这也可以通过组合OR语句来完成
WHERE HB.CreatedByDate = @ReportDateN AND
(
@Weekday = 'Monday' AND HB.Date = (@ReportDateN - 3)
)
OR
(
@Weekday != 'Monday' AND HB.Date = (@ReportDateN - 1)
)
仅供参考,请注意以下内容不起作用:
WHERE CASE @Weekday = 'Monday' THEN .... ELSE ..... END
编辑:Per op的评论如下:
WHERE HB.CreatedByDate = @ReportDateN AND
(
@Weekday = 'Monday' AND (
HB.Date = (@ReportDateN - 3) OR
HB.Date = (@ReportDateN - 2) OR
HB.Date = (@ReportDateN - 1)
)
)
OR
(
@Weekday != 'Monday' AND HB.Date = (@ReportDateN - 1)
)
这个怎么样 :
WHERE
HB.CreatedByDate = @ReportDateN AND
(
@WeekDay = 'Monday' AND HB.Date = @ReportDateN - 3
OR
HB.Date = @ReportDateN - 1
)