我试图获取员工的总时间,然后将每一行中的金额(行是在成本中心中花费的时间)除以总数,以得出员工在成本中心中花费的时间百分比。 我不断收到错误消息“子查询返回的值超过1”。
如何正确编写子查询?
样本数据: EncounterDataID,EmployeeInformationID,LocationNum,CostCenterNum,TotalHoursWorked 18378,2063,024,001,20.25 18379,2063,001,047,0.75 18380,2063,024,002,0.50 18381,2063,024,013,19.00
代码为:
选择
TempEncounterData.EmployeeInformationID,
TempEncounterData.LocationNum,
TempEncounterData.CostCenterNum,
“ TimeInCC”,
TempEncounterData.TotalHoursWorked,
投(
回合(
TempEncounterData.TotalHoursWorked /(选择SUM(TempEncounterData.TotalHoursWorked
),来自TempEncounterData AS SumHours
其中SumHours.EmployeeInformationID = TempEncounterData.EmployeeInformationID
),3
)AS小数(19,2)
)AS TotalPercentWorked,
1,
'进口',
getdate(),
'进口',
getdate()
来自SEMHC_Admin2018.dbo.TempEncounterData
加入SEMHC_Admin2018.dbo.TempEmployeeList
开启TempEmployeeList.EmployeeInformationID =
TempEncounterData.EmployeeInformationID
加入SEMHC_Admin2018.dbo.EmployeeInformation
ON EmployeeInformation.EmployeeInformationID =
TempEmployeeList.EmployeeInformationID
其中TempEncounterData.User_ID = stuff(suser_sname(),1,charindex('\',suser_sname()),'')
按TempEncounterData.EmployeeInformationID分组
EmployeeInformation.ADP_ID,EmployeeInformation.Essentia_ID
,TempEncounterData.LocationNum,TempEncounterData.CostCenterNum
,TempEncounterData.TotalHoursWorked,TempEncounterData.EncounterDataID
按TempEncounterData.EmployeeInformationID,LocationNum,CostCenterNum排序
有问题的代码是:
SELECT
SUM(TempEncounterData.TotalHoursWorked) from TempEncounterData AS SumHours
where SumHours.EmployeeInformationID =
TempEncounterData.EmployeeInformationID