缩小范围时运行总问题

时间:2018-06-25 17:58:48

标签: sql sql-server reporting-services

我的查询在没有任何限制的情况下可以正常运行:

SELECT  Reportno,
       SUM(SUM(ElapsedTime)) OVER (PARTITION BY NonProductive ORDER BY REPORTNO) AS RUNNINGTOTAL
FROM dbo.DailyOperations o
WHERE NonProductive IN (1) and WellID = 'ZCQ-5' AND JobID = 'Original' 
GROUP BY ReportNo,NonProductive
ORDER BY ReportNo;

这会给我:

Reportno    RUNNINGTOTAL
9                5
14             10.5
15              14

如果我使用ReportNo = 14运行以上查询,那么我得到

Reportno        RUNNINGTOTAL
14                  5.5

基本上,当我选择一个Reportno时,我需要查询以显示10.5(正在运行的总计)。我不确定在哪里进行修改才能使其正常工作。

此外,我正在Visual Studio SSRS中构建此代码,并尝试将Set表达式修改为:值为=RunningValue(Fields!RUNNINGTOTAL.Value,sum, "DownTime"),但没有任何改变。

1 个答案:

答案 0 :(得分:0)

您需要使用子查询,如下所示:

SELECT r.*
FROM (SELECT Reportno,
             SUM(SUM(ElapsedTime)) OVER (PARTITION BY NonProductive ORDER BY REPORTNO) AS RUNNINGTOTAL
      FROM dbo.DailyOperations o
      WHERE NonProductive IN (1) and WellID = 'ZCQ-5' AND JobID = 'Original' 
      GROUP BY ReportNo, NonProductive
     ) r
WHERE r.ReportNo = 14
ORDER BY ReportNo;