我有一个脚本,我想通过一些日期相应地从db中提取值。
我有一个返回日期的函数,这个日期可以是currentDate - 1个月,或者可以是curentDate -2个月。功能是
set @syssDate = DATEADD(month, -1, @syss_date);
我宣布了一个开始日期:
declare @start_quarter datetime
set @start_quarter = '2015-07-01';
我想在WHERE
子句中添加
case
when syssDate = start_quarter
then bu.date between '2015-01-01' and '2015-10-01'
else -- another date
我的查询如下:
declare @start_quarter datetime
set @start_quarter = '2015-01-01';
declare @actual_Date datetime
set @actual_Date = DATEADD(month, -1, @rollover_date);
select sum(t.Revenue) as Revenue from (
select sum(bu.value) as Revenue
from bus_category_for bu
join bus_category buc on buc.id=bu.bus_category_id
join bus bu on bu.id=buc.bus_id
where buc.type_id=18 and bu.id=21 and
--Here I want to add a case statement
bu.date between '2015-01-01' and '2015-10-01'
答案 0 :(得分:2)
我猜你需要这样的东西
SELECT Sum(t.Revenue) AS Revenue
FROM (SELECT Sum(bu.value) AS Revenue
FROM bus_category_for bu
JOIN bus_category buc
ON buc.id = bu.bus_category_id
JOIN bus bu
ON bu.id = buc.bus_id
WHERE buc.type_id = 18
AND bu.id = 21
AND ( ( @syssDate = @start_quarter
AND bu.date BETWEEN '2015-01-01' AND '2015-10-01' )
OR ( another date ) ))a
答案 1 :(得分:2)
尝试类似
的内容WHERE (syssDate = start_quarter AND bu.date between '2015-01-01' and '2015-10-01')
OR (yssDate <> start_quarter AND bu.date between '2016-01-01' and '2016-10-01')
通常,建议不要将between
用于日期。最好使用bu.date >= '2015-01-01' AND bu.date < '2015-10-02'
。请参阅here。