SQL,SSRS,SQL SERVER,

问题描述 投票:0回答:1

我在SQL中有一个基于事务的表,我正在创建一个SSRS报告。我有一个基于交易日期的过滤器,如下所示:

Label 7-13 value 1
Label 14-28 value 2
Label 7-28 value 3

如果用户选择1,那么我需要显示从今天起7-13天之间发生的所有交易。其他人也一样。请告诉我如何在查询级别执行此操作。

sql-server reporting-services
1个回答
1
投票

这是一种包含样本数据的方法:

declare @value int
select @value = 1

;with cte as
(
    select cast(getdate() as date) dt
    union all
    select dateadd(day, -1, dt)
    from cte
    where dt > getdate() - 30
)
select * 
from cte
where (@value = 1 and dt between DATEADD(day, -13, cast(getdate() as date)) and DATEADD(day, -7, cast(getdate() as date)))
or (@value = 2 and dt between DATEADD(day, -28, cast(getdate() as date)) and DATEADD(day, -14, cast(getdate() as date)))
or (@value = 3 and dt between DATEADD(day, -28, cast(getdate() as date)) and DATEADD(day, -7, cast(getdate() as date)))

关键是在where子句中:

WHERE (@value = 1 AND [do this if @value = 1])
OR (@value = 2 AND [do this if @value = 2])
etc.
© www.soinside.com 2019 - 2024. All rights reserved.