SSRS 2005以前的日期和时间总和

时间:2016-04-21 11:11:02

标签: sql-server reporting-services reportingservices-2005

我有一个SSRS报告,当我尝试计算前一个值时,第一行总是给出不正确的值,因为它没有先前的数字来计算。

=CDec(format(DateDiff("s",previous(Fields!SyncDate.Value),Fields!Date.Value)/60,"0.00"))

第一行的值为1059831848.62 有没有办法告诉它跳过第一行? 因为我还需要总结一下来得到总数,它计算第一行并给出一个巨大的总数。

我的查询包含以下内容:

SELECT ToppingCount, DriverName, COUNT(Pizza) AS Count, Date, SyncDate, BranchName, Branch
FROM System
WHERE     (Date BETWEEN @datefrom AND @DateTo) AND (Branch IN (@branch)) AND (SystemSource = 'newsys') AND (SystemSource <> 'oldsys')
GROUP BY Pizza, ToppingCount, DriverName, Date, SyncDate, BranchName, Branch
ORDER BY Branch, DriverName, Date

谢谢

2 个答案:

答案 0 :(得分:0)

您可以检查上一个同步日期字段的值,并输出0或空白(如果需要)。

=CDec(iif(IsNothing(previous(Fields!SyncDate.Value)), 0.0, format(DateDiff("s",previous(Fields!SyncDate.Value),Fields!Date.Value)/60,"0.00")))

如果没有row_number,您可以使用外部应用来获取查询中的最后一个同步日期并预先计算同步增量&#39;:

SELECT
    p.ToppingCount, 
    p.DriverName,
    p.Date,
    p.SyncDate,
    CASE
        WHEN i.SynchDate IS NULL THEN 0.0
        ELSE DATEDIFF(SS, l.SyncDate, p.Date) / 60.0
    END AS SyncTimeDelta,
    p.BranchName,
    p.Branch,
    COUNT(p.Pizza) AS Count
FROM System p
OUTER APPLY
(
    -- Join clause depends on how you're grouping
    -- in the report.
    SELECT TOP 1
        i.SyncDate
    FROM System i
    WHERE
        i.SystemSource = 'newsys'
        AND i.Branch = p.Branch
        AND i.DriverName = p.DriverName
        AND i.Date < p.Date
    ORDER BY
        i.SyncDate DESC
) l
WHERE
    (p.Date BETWEEN @datefrom AND @DateTo) AND
(p.Branch IN (@branch)) AND
    (p.SystemSource = 'newsys') AND
    (p.SystemSource <> 'oldsys')    -- This will never be evaluated
GROUP BY
    p.Pizza, p.ToppingCount, p.DriverName, p.Date, p.SyncDate, p.BranchName, p.Branch, l.SyncDate
ORDER BY
    p.Branch, p.DriverName, p.Date

这样您就可以在报告中对同步增量求和。我对查询知之甚少,但我已经做出了以下假设:

您希望根据给定分支和驱动程序的公式,在当前[日期]列之前的最后一个同步日期之间的时间。

如果有语法问题,我道歉,我在没有参考的情况下输入它。

答案 1 :(得分:0)

您可以像这样在查询中添加Row_Number列。

SELECT  ToppingCount,
        DriverName,
        COUNT(Pizza) AS Count,
        Date,
        SyncDate,
        BranchName,
        Branch,
        ROW_NUMBER() OVER (ORDER BY Branch,DriverName,Date) Rn
FROM    System
WHERE   (Date BETWEEN @datefrom AND @DateTo)
        AND (Branch IN (@branch))
        AND (SystemSource = 'newsys')
        AND (SystemSource <> 'oldsys')
GROUP BY Pizza,
        ToppingCount,
        DriverName,
        Date,
        SyncDate,
        BranchName,
        Branch
ORDER BY Branch,
        DriverName,
        Date

然后在计算之前检查你是否在Rn = 1上。

=iif(Fields!Rn.Value = 1, "", CDec(format(DateDiff("s",previous(Fields!SyncDate.Value),Fields!Date.Value)/60,"0.00")))

=iif(Fields!Rn.Value = 1, 0, CDec(format(DateDiff("s",previous(Fields!SyncDate.Value),Fields!Date.Value)/60,"0.00")))

如果您需要重置Row_Number以进行分组,可以添加分区依据以将数字重置为1