我正在使用 MSSQL Server,我想INNER JOIN
和GROUP
来自多个表的数据。但我得到了这个错误:
Column 'MyDB.dbo.USER.EmployeeID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
假设我有如下表格:
表股票
StockID StockName
---------------------------
1 StockOne
2 StockTwo
表用户
EmployeeID EmployeeName
---------------------------
A001 ABC
A002 ABCD
表申请
RequisitionID RequestDetailsID RequestNumber EmployeeID
---------------------------------------------------------------
1 1 RN001 A001
2 2 RN001 A001
3 3 RN002 A002
4 4 RN003 A001
表 REQUISITION_DETAILS
RequestDetailsID EmployeeID StockID
------------------------------------------
1 A001 1
2 A001 2
3 A002 1
4 A001 2
这是我的SQL 语法:
SELECT *
FROM [MyDB].[dbo].[USER] A
INNER JOIN
[MyDB].[dbo].[REQUISITION] B
ON
B.EmployeeID = A.EmployeeID
INNER JOIN
[MyDB].[dbo].[REQUISITION_DETAILS] C
ON
C.RequestDetailsID = B.RequestDetailsID
INNER JOIN [MyDB].[dbo].[STOCK] D
ON
D.StockID = C.StockID
WHERE EmployeeID = '$EpID'
GROUP BY B.RequestNumber
预期输出
RequestNumber RequestDetailsID EmployeeID EmployeeName StockID
------------------------------------------------------------------------------
RN001 1, 2 A001 ABC 1, 2
RN002 3 A002 ABCD 1
RN003 4 A001 ABC 2