2013-08-19 91 views
0

我使用SSRS 2005來創建報告,以顯示不同類別小時的款項(註冊時間,加班等)多行總計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 

我總學時罰款,但也希望能夠把它打破,只有加班時間的專欄,只有正常的時間和一切的總數。有沒有一種簡單/乾淨的方式來做到這一點,任何人都知道?我嘗試過使用不同的數據集,但它並沒有達到我的預期,而像子查詢之類的東西似乎會變得雜亂和冗餘。

回答

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; 
+0

非常感謝,工作得很好。 – mrshickadance

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 

,讓你的總數爲每種類型(普通,加班等),然後在您的報告中創建總和

+0

我認爲如果我不把一些加班加在一起,這會非常有效。儘管這很好,但它使我能夠與分組比賽。謝謝 – mrshickadance

+0

你非常歡迎,祝你好運 – Hedinn

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聲明提取您需要的組。

+0

我碰巧使用戈登的例子,但這看起來差不多相同 – mrshickadance

+0

是的,幾乎是相同的想法。他毆打我幾秒鐘的答案,所以只有公平的他才能得到公認的答案。感謝更新! –