SSRS 2005中的多行总计

时间:2013-08-19 13:41:34

标签: sql tsql reporting-services reportingservices-2005

我正在使用SSRS 2005创建一份报告,显示不同类别的小时数(注册时间,加班时间等)

SELECT OGL.PACostCenter, vpt.LL6, sum((vpt.timeinseconds*1.0)/3600) [Hours]
FROM totals as vpt
INNER JOIN OracleLookup OGL on vpt.LL6 = OGL.OracleCostCenter COLLATE SQL_Latin1_General_CP1_CI_AI
WHERE vpt.DATE BETWEEN @StartDate AND @EndDate
AND vpt.PAYCODENAME in ('519-H-Overtime 1.0',
            '519-H-Holiday OT 1.5',
            '519-H-Overtime 1.5',
            '519-H-Overtime 2.0',
            '519-H-Regular')
GROUP BY OGL.PayrollAccount, vpt.LL6
ORDER BY  OGL.PayrollAccount, vpt.LL6

我的总时数很好,但也希望能够将其分解并且只有加班时间的列,而且只有常规时间以及所有内容的总数。是否有一种简单/干净的方式来做任何人都知道的事情?我尝试使用不同的数据集,但它没有按照我的预期进行操作,像子查询这样的东西似乎真的很混乱和多余。

3 个答案:

答案 0 :(得分:2)

是的,您可以使用条件求和:

SELECT OGL.PACostCenter, vpt.LL6, sum((vpt.timeinseconds*1.0)/3600) [Hours],
       sum(case when vpt.PAYCODENAME in ('519-H-Overtime 1.0', '519-H-Holiday OT 1.5',
                                         '519-H-Overtime 1.5', '519-H-Overtime 2.0'
                                        )
                then (vpt.timeinseconds*1.0)/3600
                else 0
           end) as OvertimeHours,
       sum(case when vpt.PAYCODENAME in ('519-H-Regular')
                then (vpt.timeinseconds*1.0)/3600
                else 0
           end) as RegularHours,
FROM totals as vpt
INNER JOIN OracleLookup OGL on vpt.LL6 = OGL.OracleCostCenter COLLATE SQL_Latin1_General_CP1_CI_AI
WHERE vpt.DATE BETWEEN @StartDate AND @EndDate
AND vpt.PAYCODENAME in ('519-H-Overtime 1.0',
            '519-H-Holiday OT 1.5',
            '519-H-Overtime 1.5',
            '519-H-Overtime 2.0',
            '519-H-Regular')
GROUP BY OGL.PayrollAccount, vpt.LL6
ORDER BY  OGL.PayrollAccount, vpt.LL6;

答案 1 :(得分:1)

假设您可以控制该数据集查询,您应该可以执行以下操作:

SELECT OGL.PACostCenter, vpt.LL6
  , sum((vpt.timeinseconds*1.0)/3600) [Hours] -- your initial total
  , sum(case when vpt.PAYCODENAME = '519-H-Overtime 1.0'
    then vpt.timeinseconds*1.0 else null end) / 3600 [OT1]
  , sum(case when vpt.PAYCODENAME = '519-H-Overtime 1.5'
    then vpt.timeinseconds*1.0 else null end) / 3600 [OT1pt5]
  -- further SUM/CASE as required
FROM totals as vpt
INNER JOIN OracleLookup OGL on vpt.LL6 = OGL.OracleCostCenter COLLATE SQL_Latin1_General_CP1_CI_AI
WHERE vpt.DATE BETWEEN @StartDate AND @EndDate
AND vpt.PAYCODENAME in ('519-H-Overtime 1.0',
            '519-H-Holiday OT 1.5',
            '519-H-Overtime 1.5',
            '519-H-Overtime 2.0',
            '519-H-Regular')
GROUP BY OGL.PayrollAccount, vpt.LL6
ORDER BY  OGL.PayrollAccount, vpt.LL6

即。 Appy SUMCASE语句,用于提取您需要的组。

答案 2 :(得分:0)

因为你正在使用SSRS我建议你这样做

SELECT OGL.PACostCenter, vpt.PAYCODENAME, vpt.LL6, sum((vpt.timeinseconds*1.0)/3600) [Hours]
FROM totals as vpt
INNER JOIN OracleLookup OGL on vpt.LL6 = OGL.OracleCostCenter COLLATE SQL_Latin1_General_CP1_CI_AI
WHERE vpt.DATE BETWEEN @StartDate AND @EndDate
AND vpt.PAYCODENAME in ('519-H-Overtime 1.0',
        '519-H-Holiday OT 1.5',
        '519-H-Overtime 1.5',
        '519-H-Overtime 2.0',
        '519-H-Regular')
GROUP BY OGL.PayrollAccount, vpt.PAYCODENAME, vpt.LL6
ORDER BY  OGL.PayrollAccount, vpt.LL6

为您提供每种类型的总计(常规,加班等),然后在报告中创建总和