SQL Server - 连接表时组连接

时间:2016-09-13 09:36:17

标签: sql sql-server

我是SQL Server的初学者。

我有成功的工作:

select top 100 tpeople.firstname,tpeople.lastName,tpeople.city, STUFF(( SELECT  ', ' + cast(T2.education as varchar(50))
                    FROM   tpeopleEducation T2
                    where  tpeople.GUID = T2.PeopleGUID
                    FOR XML PATH('')
                ), 1,1, '') as education

    from tpeople full join tpeopleEducation
    on tpeople.GUID = tpeopleEducation.PeopleGUID
    group by tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City

哪个输出:

firstname|lastname|city|education
Joe       Doe      NYC  MIT,Harvard
John      Smith    LA   NYU

我正试图让它发挥作用。在这里我需要像下面这样加入,以便每个人(tpeople)访问属性(tAttributes):

select top 100 tpeople.firstname,tpeople.lastName,tpeople.city

   ,STUFF(( SELECT  ', ' + cast(T2.attribute as varchar(50))
                    FROM    tattributes T2 
                   where  tpeoplecluendex.AttributeGUID = T2.GUID
                    FOR XML PATH('')
                ), 1,1, '') as attributes       

   from tpeople 

   join tPeopleCluendex on tPeopleCluendex.CPSGUID = tpeople.GUID


   join tAttributes on tAttributes.guid = tPeopleCluendex.AttributeGUID

   group by tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,tAttributes.GUID,tPeopleCluendex.AttributeGUID

输出是:

firstname|lastname|city|attributes
Joe       Doe      NYC  test1
Joe       Doe      NYC  test2
John      Smith    LA   test1

为什么不喜欢:

firstname|lastname|city|attributes
Joe       Doe      NYC  test1,test2
John      Smith    LA   test1

对不起,如果我写了一些废话。

任何可以指出正确方向的SQL Server大师?

如果我要添加更多信息,请告诉我。我会更新我的问题。

谢谢,

更新1:

select top 100 tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,tAttributes.GUID,tPeopleCluendex.AttributeGUID

   ,STUFF(( SELECT  ', ' + cast(T2.attribute as varchar(50))
                    FROM    tattributes T2 
                   where  tpeoplecluendex.AttributeGUID = T2.GUID
                    FOR XML PATH('')
                ), 1,1, '') as attributes       

   from tpeople 

   join tPeopleCluendex on tPeopleCluendex.CPSGUID = tpeople.GUID


   join tAttributes on tAttributes.guid = tPeopleCluendex.AttributeGUID

   group by tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,tAttributes.GUID,tPeopleCluendex.AttributeGUID

输出是:

 GUID                                 FirstName                                          LastName                                           City                                               GUID                                 AttributeGUID                        attributes
    ------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------------ ------------------------------------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1E92D80A-1859-4A2A-AE69-00003FF7190B Joe                                               Doe                                                                                                2972FC47-8511-4429-BBA3-00E515E3769D 2972FC47-8511-4429-BBA3-00E515E3769D  test1
    1E92D80A-1859-4A2A-AE69-00003FF7190B Joe                                               Doe                                                                                                E317A420-1B25-4C6F-B8B3-164F185851E0 E317A420-1B25-4C6F-B8B3-164F185851E0  test2

更新2:

这样做:

select  top 100 tpeople.FirstName,tpeople.LastName,tpeople.City,
       STUFF(( SELECT  ', ' + cast(tAttributes.attribute as varchar(50))
       FROM    tAttributes, tpeoplecluendex
       where tattributes.GUID = tPeopleCluendex.AttributeGUID  and tPeopleCluendex.CPSGUID = tpeople.GUID
       group by attribute FOR XML PATH('')), 1,1, '') as attributes       
from tpeople 
group by tpeople.GUID,FirstName,LastName,City

2 个答案:

答案 0 :(得分:0)

尝试按列打印所有组以查看差异 -

select top 100 tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,tAttributes.GUID,tPeopleCluendex.AttributeGUID

   ,STUFF(( SELECT  ', ' + cast(T2.attribute as varchar(50))
                    FROM    tattributes T2 
                   where  tpeoplecluendex.AttributeGUID = T2.GUID
                    FOR XML PATH('')
                ), 1,1, '') as attributes       

   from tpeople 

   join tPeopleCluendex on tPeopleCluendex.CPSGUID = tpeople.GUID


   join tAttributes on tAttributes.guid = tPeopleCluendex.AttributeGUID

   group by tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,tAttributes.GUID,tPeopleCluendex.AttributeGUID

答案 1 :(得分:0)

连接时将GUID分组

select  tpeople.GUID,tpeople.FirstName,tpeople.LastName,tpeople.City,
       STUFF(( SELECT  ', ' + cast(tAttributes.attribute as varchar(50))
       FROM    tAttributes, tpeoplecluendex
       where AttributeGUID = GUID and tPeopleCluendex.CPSGUID = tpeople.GUID
       group by attribute FOR XML PATH('')), 1,1, '') as attributes       
from tpeople 
group by tpeople.GUID,FirstName,LastName,City
相关问题