SQL服务器在多列上的东西

时间:2016-04-26 14:05:32

标签: sql sql-server

我想为同一个ID连接多个列的值。 我设法连接第一列,但是当第二列尝试相同的语法时,我有一个错误&#34;多部分标识符EnqAct。[ActionID]无法绑定。&#34; < / p>

Example_Data_Result

在这里,我设法将多个MachineName组合在一起,如&#34; Column1&#34;但我无法在自己的专栏中对MachineName和Description进行分组

我的工作查询:

SELECT Enq.[EnquiryID],
       Enq.[CustomerName],
       DetPrio.[Description],
       Stuff((SELECT ', ' + Mach.[MachineName]
              FROM   [dbo].[Machine] Mach
                     INNER JOIN [dbo].[MachineEnquiry] MachEnq
                             ON Mach.[MachineID] = MachEnq.[MachineID]
              WHERE  Enq.[EnquiryID] = MachEnq.[EnquiryID]
              FOR XML PATH('')), 1, 2, ''),
       DetAct.[Description]
FROM   [dbo].[Enquiry] Enq
       INNER JOIN [dbo].[EnquiryAction] EnqAct
               ON EnqAct.[EnquiryID] = Enq.[EnquiryID]
       INNER JOIN [dbo].[DetailsAction] DetAct
               ON DetAct.[ActionID] = EnqAct.[ActionID]
       INNER JOIN [dbo].[DetailsPriority] DetPrio
               ON DetPrio.[PriorityID] = Enq.[Priority]
GROUP  BY Enq.[EnquiryID],
          Enq.[CustomerName],
          DetPrio.[Description],
          DetAct.[Description]  

我的非工作查询:

SELECT Enq.[EnquiryID],
       Enq.[CustomerName],
       DetPrio.[Description],
       Stuff((SELECT ', ' + Mach.[MachineName]
              FROM   [dbo].[Machine] Mach
                     INNER JOIN [dbo].[MachineEnquiry] MachEnq
                             ON Mach.[MachineID] = MachEnq.[MachineID]
              WHERE  Enq.[EnquiryID] = MachEnq.[EnquiryID]
              FOR XML PATH('')), 1, 2, ''),
       Stuff((SELECT ', ' + DetAct.[Description]
              FROM   [dbo].[DetailsAction] DetAct
                     INNER JOIN [dbo].[EnquiryAction] EnqAct
                             ON EnqAct.[ActionID] = DetAtc.[ActionID]
              WHERE  Enq.[EnquiryID] = EnqAct.[EnquiryID]
              FOR XML PATH('')), 1, 2, '')
FROM   [dbo].[Enquiry] Enq
       INNER JOIN [dbo].[DetailsPriority] DetPrio
               ON DetPrio.[PriorityID] = Enq.[Priority]
GROUP  BY Enq.[EnquiryID],
          Enq.[CustomerName],
          DetPrio.[Description]  

两者都以相同的方式工作,我有一个表查询,它将具有EnquiryID。 然后在我的表EnquiryAction或MachineEnquiry我将有实体具有EnquiryID和动作/机器ID 然后在我的DetailsAction / Machine表中,我将获得Action / Machine ID,以及我想要获取和连接的字符串。

为什么我有错误,是否有可能实现我想要做的事情?

0 个答案:

没有答案