SQL Server 2008 R2 - 存储最高结果并在 SSRS 中使用

SQL Server 2008 R2 - store top result and use in SSRS

我有一份 SSRS 报告,其中显示了自收到投诉以来经过的总天数。这个SQL查询的是今天的日期和上次收到投诉的日期的差值。

SELECT DATEDIFF(day, MAX(complaints.ComplaintReceived1Date),CURRENT_TIMESTAMP) as total 
FROM complaints WITH (nolock)

例如,如果将此设置为 30(天),然后在我的 SSRS 报告中收到投诉,我想显示 30 作为之前没有投诉记录的天数。有没有办法存储以前的结果并调用这些数据?也许是临时工 table?

您已经将其存储在 SQL 查询引用的 table 中。

我会从那里取回它:

; with previouscomplaint as (

select 
complaintreceived1date,
RN = ROW_NUMBER() over (partition by complaintreceived1dateorder by complaintreceived1date desc))


select datediff(day,complaintreceived1date,current_timestamp) as previoustotal from previouscomplaint where RN=2

如果您想要两行之间的日期,请进行第二条语句:

select datediff(day, (select complaintreceived1date from previouscomplaint where rn = 2),(select complaintreceived1date from previouscomplaint where rn = 1)) as previoustotal

这没有经过测试,但应该可以。