2015-06-03 106 views
0

我有一个SSRS报告,显示自收到投诉后的总天数。此SQL查询是最近收到投诉的当天日期和日期之间的差异。SQL Server 2008 R2 - 存储最佳结果并在SSRS中使用

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

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

回答

0

您已经将它存储在您的SQL查询引用的表中。

我只想从那里检索:

; 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 

这并没有进行测试,但应该工作。

+0

您好!感谢@Eric Hauenstein您的评论 - 我不会将此信息存储在表格中,我只有'投诉收到'表格,而不是'以前的投诉'表格。我只是简单地找到最近收到的投诉和今天的日期之间的天数。例如目前设置为30,但在此之前设置为40,所以在SSRS中,我想在我的报告中显示“记录没有投诉的天数= 40”,但不知道如何实现。 – sql2015

+0

您正在从“投诉”表中进行选择,对吗?它应该包含您收到的所有投诉的清单,对吗?如果是这样,那么我发布的代码应该在现有投诉之前检索投诉*,我认为这是您想要的。 –

+0

感谢@Eric Hauenstein这正是我所追求的 – sql2015

相关问题