1

我正在使用 MSSQL Server,我想INNER JOINGROUP来自多个表的数据。但我得到了这个错误:

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
4

1 回答 1

1

由于几个原因,这是一个棘手的查询。首先,SQL Server 有一种相当痛苦的方式来进行聚合字符串连接。然后,您会遇到连接不同表中的请求编号和员工的问题。

据我所知,您只需要两个表,requisition并且requisition_details. 以下查询应该接近您的需要:

select distinct r.RequestNumber, r.EmployeeId,
       stuff((select ', ' + cast(rd.RequestDetailsId as varchar(255))
              from requisition r2 join
                   requisition_details rd
                   on r2.RequestDetailsID = rd.RequestDetailsID
              where r2.RequestNumber = r.RequestNumber and r2.EmployeeId = r.EmployeeId
              for xml path ('')
             ), 1, 2, '') as RequestDetailIds,
       stuff((select ', ' + cast(rd.StockId as varchar(255))
              from requisition r2 join
                   requisition_details rd
                   on r2.RequestDetailsID = rd.RequestDetailsID
              where r2.RequestNumber = r.RequestNumber and r2.EmployeeId = r.EmployeeId
              for xml path ('')
             ), 1, 2, '') as StockIds
from requisition r

编辑:

要插入换行符,我只需使用:

       stuff((select '
' + cast(rd.RequestDetailsId as varchar(255))
              from requisition r2 join
                   requisition_details rd
                   on r2.RequestDetailsID = rd.RequestDetailsID
              where r2.RequestNumber = r.RequestNumber and r2.EmployeeId = r.EmployeeId
              for xml path ('')
             ), 1, 2, '') as RequestDetailIds,
于 2014-12-02T04:10:20.907 回答