【问题标题】:SQL Server 2008 R2 - store top result and use in SSRSSQL Server 2008 R2 - 存储顶部结果并在 SSRS 中使用
【发布时间】:2015-08-17 13:18:18
【问题描述】:

我有一份 SSRS 报告,其中显示自收到投诉以来经过的总天数。此 SQL 查询是今天日期与上次收到投诉日期之间的差异。

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

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

【问题讨论】:

    标签: tsql reporting-services sql-server-2008-r2 temp-tables


    【解决方案1】:

    您已经将它存储在您的 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
    

    这未经测试,但应该可以工作。

    【讨论】:

    • 您好,感谢@Eric Hauenstein 的评论 - 我没有将其存储在表格中,我只有“收到的投诉”表格,而不是“先前的投诉”表格。我只是在查找上次收到的投诉和今天的日期之间的天数。例如,它当前设置为 30,但在此之前设置为 40,因此在 SSRS 中,我想在我的报告中显示“没有投诉的记录天数 = 40”,不确定如何实现。
    • 您是从“投诉”表中选择的,对吧?它应该包含您收到的所有投诉的清单,对吗?如果是这样,那么我发布的代码应该在当前投诉之前检索投诉,我认为这就是您想要的。
    • 感谢@Eric Hauenstein 这正是我所追求的
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    相关资源
    最近更新 更多