我正在尝试与两个按相同ID分组的表格进行联接,以获得周和月结果。
当我尝试执行查询时,SQL Server会返回以下消息:
Msg 8120,Level 16,State 1,Line 3
列'P.WEEK'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
这是查询:
Select
C.[id],
P.[WEEK],
SUM(C.[value1]+C.[value2]) AS MONTH
FROM
[Schema].[table] C
JOIN
(Select
R.[Equipo],
SUM(R.[value1]+R.[value2]) AS WEEK
FROM
[Schema].[Table] R
WHERE
R.[Horario]='TODO EL DIA'
AND R.[date] BETWEEN CONVERT(VARCHAR(10),DATEADD(DAY, -8, GETDATE()),120) AND CONVERT(VARCHAR(10),DATEADD(DAY, -1, GETDATE()),120)
GROUP BY R.[id]) P ON C.[id] = P.[id]
WHERE
C.[date] BETWEEN CONVERT(VARCHAR(10),DATEADD(DAY, -32, GETDATE()),120) AND CONVERT(VARCHAR(10),DATEADD(DAY, -1, GETDATE()),120)
AND C.[Horario]='TODO EL DIA'
GROUP BY
C.[id]
为什么?当我在MySQL中执行此操作时,查询将返回正确的值。