0

我正在使用 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

我的总小时数很好,但也希望能够将其分解并仅列出加班时间,只有正常时间以及所有内容的总数。有没有人知道的简单/干净的方法来做到这一点?我尝试使用不同的数据集,但它没有达到我的预期,像子查询这样的东西看起来真的很混乱和多余。

4

3 回答 3

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;
于 2013-08-19T13:49:16.327 回答
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

SUM适用于CASE提取您需要的组的语句。

于 2013-08-19T13:51:23.767 回答
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

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

于 2013-08-19T13:51:16.130 回答