如何获取不同代码的总时间百分比
我有一个名为“时间表”的表,数据如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)