如何获取不同代码的总时间百分比

发布于 2024-11-03 20:27:50 字数 887 浏览 0 评论 0原文

我有一个名为“时间表”的表,数据如下所示:

Hours   Code    FiMonth  Mgr
16      OT      2011M1   Sam
0.25    DT      2011M1   Peter
1.75    HNE     2011M1   george
6.5     DT      2010M1   Peter
3.25    OT      2010M1   Sam
0.5     DT      2010M2   Sam
30      HNE     2011M1   John

现在我必须计算时间%,获取该值的公式是:

时间% = 总和(DT+OT) 小时/(DT+OT+HNE) 小时

我将查询编写为:

SELECT  ( ( SELECT  SUM(CONVERT(FLOAT, [Hours])) AS [Hours]
            FROM    Timetable d
            WHERE   d.code IN ( 'OT','DT') 
          )
          / ( SELECT    SUM(CONVERT(FLOAT, [Hours])) AS [Hours]
              FROM      Timetable t
              WHERE     t.code IN ( 'OT', 'DT','HNE')
           ) ) AS Time%
           FROM    Timetable
           GROUP BY Mgr

我必须在我的存储过程中使用这个值,并且我有很多这样的列。因此,我不想为一栏编写这么多代码。

有更好的方法吗?

I have a table called Timetable and the data looks like:

Hours   Code    FiMonth  Mgr
16      OT      2011M1   Sam
0.25    DT      2011M1   Peter
1.75    HNE     2011M1   george
6.5     DT      2010M1   Peter
3.25    OT      2010M1   Sam
0.5     DT      2010M2   Sam
30      HNE     2011M1   John

Now I have to calculate the time% and the formula to get that value is:

Time% = Sum(DT+OT) Hours/(DT+OT+HNE)Hours

I wrote the query as:

SELECT  ( ( SELECT  SUM(CONVERT(FLOAT, [Hours])) AS [Hours]
            FROM    Timetable d
            WHERE   d.code IN ( 'OT','DT') 
          )
          / ( SELECT    SUM(CONVERT(FLOAT, [Hours])) AS [Hours]
              FROM      Timetable t
              WHERE     t.code IN ( 'OT', 'DT','HNE')
           ) ) AS Time%
           FROM    Timetable
           GROUP BY Mgr

I have to use this value in my Stored Procedure and I have many Columns like this. So, I don't want to write this much code for one column.

Is there a better way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-11-10 20:27:50
WITH Timetable([Hours],code,FiMonth,Mgr) as
(
SELECT 16,'OT','2011M1','Sam' UNION ALL
SELECT 0.25,'DT','2011M1','Peter' UNION ALL
SELECT 1.75,'HNE','2011M1','george' UNION ALL
SELECT 6.5,'DT','2010M1','Peter' UNION ALL
SELECT 3.25,'OT','2010M1','Sam' UNION ALL
SELECT 0.5,'DT','2010M2','Sam' UNION ALL
SELECT 30,'HNE','2011M1','John'
)
SELECT Mgr,
       SUM(CASE
             WHEN code IN ( 'OT', 'DT' ) THEN CAST([Hours] AS FLOAT)
             ELSE 0
           END)/ NULLIF(SUM(CAST([Hours] AS FLOAT)),0) AS [Time%]
FROM   Timetable t
WHERE  code IN ( 'OT', 'DT', 'HNE' )
GROUP  BY Mgr  
WITH Timetable([Hours],code,FiMonth,Mgr) as
(
SELECT 16,'OT','2011M1','Sam' UNION ALL
SELECT 0.25,'DT','2011M1','Peter' UNION ALL
SELECT 1.75,'HNE','2011M1','george' UNION ALL
SELECT 6.5,'DT','2010M1','Peter' UNION ALL
SELECT 3.25,'OT','2010M1','Sam' UNION ALL
SELECT 0.5,'DT','2010M2','Sam' UNION ALL
SELECT 30,'HNE','2011M1','John'
)
SELECT Mgr,
       SUM(CASE
             WHEN code IN ( 'OT', 'DT' ) THEN CAST([Hours] AS FLOAT)
             ELSE 0
           END)/ NULLIF(SUM(CAST([Hours] AS FLOAT)),0) AS [Time%]
FROM   Timetable t
WHERE  code IN ( 'OT', 'DT', 'HNE' )
GROUP  BY Mgr  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文