2016-09-13 4 views
0

私は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 

私はこれが動作するように成功を持っています。

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 

出力は次のとおりです:ここで私は一人あたりの属性(tAttributes)(tpeople)へのアクセスを得るためには、以下のように参加するために必要な

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 Gurusはありますか?

詳細情報を追加する必要がある場合はお知らせください。私は私の質問を更新します。

おかげで、

アップデート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 
+1

多分を連結しながら、 )。それはグループ内の競合の原因となっている他の列かもしれませんが、おそらくグループ化の問題です。 – ZLK

+1

グループからtAttributes.GUID、tPeopleCluendex.AttributeGUIDを削除します。 –

答えて

0

違いを見るためにすべてのGROUP BY列を印刷しよう -

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 
+0

質問を出力に照会して更新しました。 1行の属性をカンマで区切って表示するには、どのように調整する必要がありますか?感謝!!!! –

0

グループGUID彼らは異なるGUIDを持っていて、他のものの間(の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 
+0

ありがとう、ただし、動作しません....メッセージ209、レベル16、状態1、行4 あいまいな列名 'GUID'。何か案は? –

+0

更新プログラム2を参照してください。 –

関連する問題