Question

I am using SSRS 2005 to create a report to show sums of hours for different categories (Reg Hours, Overtime, etc.)

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

I have the total hours fine, but also want to be able to break it down and have columns for only overtime, and only regular hours as well as the total for everything. Is there an easy/clean way to do this that anyone knows of? I tried using different data sets but it wasn't doing what I expected, and something like a subquery seemed like it would be really messy and redundant.

Was it helpful?

Solution

Yes, you can use conditional summation:

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;

OTHER TIPS

Assuming you have control over that Dataset query, you should be able to do something like:

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

i.e. Appy SUM to CASE statements that extract the groups you require.

since you are using SSRS I would reccomend that you do it like this

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

which gives you totals for each type (regular, overtime, etc.) and then create the total sum in your report

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top