从xml路径获取特定列

时间:2015-08-21 09:17:30

标签: sql sql-server tsql for-xml-path

我在这里有这个问题:

SELECT      
    T.AKSIONERET, 
    sd.Identification_No, sd.Date_Of_Incorp, 
    sd.Ownership_Desc, sd.Ownership_Code, 
    sd.Subject_Name_Code, sd.Subject_Name, 
    sd.Subject_Type_Code, 
    LEFT(sd.Object_Desc, 3999) AS Object_Desc, 
    sd.Subject_Status_Name, sd.Subject_Name, 
    nr2.NACE_Code, nr2.NACE_PARENT, nr2.Description, 
    sd.Subject_Type_Name, sd.Subject_Status_Name, 
    sd.Subject_Type_Name, f.Place_Of_Registration, sd.Object_Desc
FROM            
    NRC_Subject_DEF AS sd 
LEFT JOIN 
    NRC_Nace_REV2 AS nr2 ON nr2.Subject_DEF_Code = sd.Subject_DEF_Code 
LEFT JOIN 
    NRC_ForeignCompany AS f ON sd.ForeignCompany_Code = f.ForeignCompany_Code             
LEFT JOIN 
    (SELECT 
         SDP.Subject_DEF_Code,
         (SELECT 
              SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
          FROM 
              NRC_PeopleAddress P 
          INNER JOIN 
              NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
          WHERE 
              SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code                         
          GROUP BY 
              SDP1.Subject_DEF_Code,  P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc 
          FOR XML PATH('')) AS AKSIONERET
      FROM 
          NRC_SubjectDefPeople SDP 
      WHERE 
          SDP.Groyp_Type IN (1,2)           
      GROUP BY 
          SDP.Subject_DEF_Code) AS T ON T.Subject_DEF_Code = SD.SUBJECT_DEF_CODE

我需要检索,而不是T.AKSIONERET我从for xml路径获取的FullAksionerCitizenship

我知道我必须做一些其他的SELECT,但我不断收到错误,语法错误。

有人可以帮忙吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

我很确定,可能有一个更好的设计,我无法在测试数据上运行此功能,但这可能会有效:

SELECT      
    T.AKSIONERET, 
    T2.FullAksionerCitizenship,
    sd.Identification_No, sd.Date_Of_Incorp, 
    sd.Ownership_Desc, sd.Ownership_Code, 
    sd.Subject_Name_Code, sd.Subject_Name, 
    sd.Subject_Type_Code, 
    LEFT(sd.Object_Desc, 3999) AS Object_Desc, 
    sd.Subject_Status_Name, sd.Subject_Name, 
    nr2.NACE_Code, nr2.NACE_PARENT, nr2.Description, 
    sd.Subject_Type_Name, sd.Subject_Status_Name, 
    sd.Subject_Type_Name, f.Place_Of_Registration, sd.Object_Desc
FROM            
    NRC_Subject_DEF AS sd 
LEFT JOIN 
    NRC_Nace_REV2 AS nr2 ON nr2.Subject_DEF_Code = sd.Subject_DEF_Code 
LEFT JOIN 
    NRC_ForeignCompany AS f ON sd.ForeignCompany_Code = f.ForeignCompany_Code             
LEFT JOIN 
    (SELECT 
         SDP.Subject_DEF_Code,
         (SELECT 
              SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
          FROM 
              NRC_PeopleAddress P 
          INNER JOIN 
              NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
          WHERE 
              SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code                         
          GROUP BY 
              SDP1.Subject_DEF_Code,  P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc 
          FOR XML PATH('')) AS AKSIONERET
      FROM 
          NRC_SubjectDefPeople SDP 
      WHERE 
          SDP.Groyp_Type IN (1,2)           
      GROUP BY 
          SDP.Subject_DEF_Code) AS T ON T.Subject_DEF_Code = SD.SUBJECT_DEF_CODE
--here starts the new block (difficult for me without knowing your db...)
LEFT JOIN 
    (SELECT 
         (SELECT 
              SDP1.Subject_DEF_Code, P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc AS FullAksionerCitizenship
          FROM 
              NRC_PeopleAddress P 
          INNER JOIN 
              NRC_SubjectDefPeople SDP1 ON SDP1.People_Code = P.People_Code
          WHERE 
              SDP1.Subject_DEF_Code = SDP.Subject_DEF_Code                         
          GROUP BY 
              SDP1.Subject_DEF_Code,  P.First_Name + ' ' + P.Last_Name + '-' + P.Citizenship_Desc 
          ) AS FullAksionerCitizenship
      FROM 
          NRC_SubjectDefPeople SDP 
      WHERE 
          SDP.Groyp_Type IN (1,2)           
      GROUP BY 
          SDP.Subject_DEF_Code) AS T2 ON T2.Subject_DEF_Code = SD.SUBJECT_DEF_CODE