我的代码会根据描述对小时列的数据求和,然后根据主管列汇总小时数。我想添加一个计算列,它将取第一个总和除以第二个总和给我一个百分比。这是我的代码,我认为是百分之列的正确路径,但我不能让它工作。任何帮助表示赞赏!
SELECT sap.Description,
Sum(main.Hours) AS SumOfHours,
main.supervisor1email AS SupervisorEmail,
(SELECT SUM(sub.hours)
FROM v_MES_TcActivities sub
WHERE sub.costctr = '106330'
AND sub.AttCode Not Like 'MEAL'
AND sub.clockin Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
AND sub.supervisor1email = main.supervisor1email
) AS TotalHours,
(SELECT Sum(subsub.hours) / TotalHours AS 'Percent'
) AS Percent
FROM v_MES_TcActivities AS main
LEFT JOIN t_SAP_AttCodes AS sap ON main.AttCode = sap.Code
WHERE main.AttCode Not Like 'MEAL'
AND main.CostCtr Like '106330'
AND main.ClockIn Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
GROUP BY sap.Description,
main.supervisor1email,
Percent
ORDER BY main.supervisor1email
答案 0 :(得分:0)
你也可以试试这个
SELECT Description,
SumOfHours,
SupervisorEmail,
TotalHours,
SumSubsubHr/TotalHours AS 'Percentage'
FROM (
SELECT sap.Description,
Sum(main.Hours) AS SumOfHours,
main.supervisor1email AS SupervisorEmail,
(SELECT SUM(sub.hours)
FROM v_MES_TcActivities sub
WHERE sub.costctr='106330' AND sub.AttCode Not Like 'MEAL' AND sub.clockin Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
AND sub.supervisor1email = main.supervisor1email
) AS TotalHours,
Sum(sub.hours) AS 'SumSubsubHr'
FROM v_MES_TcActivities AS main
LEFT JOIN t_SAP_AttCodes AS sap ON main.AttCode = sap.Code
WHERE main.AttCode Not Like 'MEAL'
AND main.CostCtr Like '106330'
AND main.ClockIn Between dateadd(day, 1-datepart(dw, getdate()), CONVERT(date,getdate())) and dateadd(day, 8-datepart(dw, getdate()), CONVERT(date,getdate()))
GROUP BY sap.Description, main.supervisor1email
)t