sql-server - 当 Duration_Hours 是另一个数据库表的 LEFT JOIN 时,如何求和?
问题描述
我想生成一份报告,总结每位员工的总工作时间。
下面是我的 SQL 查询
SELECT WO_MaidName
,Maid_Name AS WO_selectMaidName
,Appointment_Duration
,Duration_Hours AS Appointment_selectDuration
FROM Appointments A
LEFT JOIN Maid
ON Maid_ID = WO_MaidName
LEFT JOIN Duration
ON Duration_ID = Appointment_Duration
WHERE DATEPART(m, Appointment_DateTime) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(yyyy, Appointment_DateTime) = DATEPART(yyyy, DATEADD(m, -1, getdate()))
AND (A.IsDelete = 0
OR A.IsDelete IS NULL
)
ORDER BY Appointment_ID DESC
输出如下,仅显示工作时间,不添加这两个值
当它是另一个数据库表时,如何SUM
让报告列出每个员工的总工作时间?Duration_Hours
LEFT JOIN
编辑:我试过这个查询,但给了我这个:
'列 'Maid.Maid_Name' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
SELECT WO_MaidName
,Maid_Name AS WO_selectMaidName
,Appointment_Duration
,SUM(Duration_Hours) AS Appointment_selectDuration
FROM Appointments A
LEFT JOIN Maid
ON Maid_ID = WO_MaidName
LEFT JOIN Duration
ON Duration_ID = Appointment_Duration
WHERE DATEPART(m, Appointment_DateTime) = DATEPART(m, DATEADD(m, -1, getdate()))
AND DATEPART(yyyy, Appointment_DateTime) = DATEPART(yyyy, DATEADD(m, -1, getdate()))
AND (A.IsDelete = 0
OR A.IsDelete IS NULL
)
GROUP BY WO_MaidName
ORDER BY Appointment_ID DESC
如何解决这个问题?
解决方案
您得到的错误是告诉您该怎么做:
'Column 'Maid.Maid_Name' 在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。'
select
查询中执行聚合的所有列都需要聚合或在group by
. 您需要将Maid.Maid_Name
和添加Appointment_Duration
到您的group by
.
推荐阅读
- mongodb - 春季数据mongo过渡
- c# - 没有为 Oracle.DataAccess.Client 错误安装 DDEX 提供程序
- many-to-many - jhipster中多对多关系的问题
- rest - QlikSense - REST API 链式调用 - 脚本
- go - 空嵌套结构
- c# - 运行 .NET Core API 项目会出现错误 502.5 - 进程失败
- java - Java webapp如何在内网发布其DNS名称
- client - HBase - 将 Java 客户端从 Eclipse 连接到 EC2 实例上的 HBase 时出错
- react-native - 在调试模式下工作时反应本机胜利图表在发布模式下不工作
- javascript - 在按钮单击时以 4 角滚动 div