2016-02-03 47 views
1

我的問題看起來非常直截了當,但我正在努力掌握實現所需輸出的更簡單方法。以多列位置作爲列表返回一行

問題:我有一個表中的條目。這些條目由ID列標識。他們加入一張表格,顯示他們持有或持有的特定志願者職位。如果他們被確定爲擁有多個職位,他們的記錄會多次返回。

解決方案:我想在一列中列出它們的位置,以便它們基於ID列的條目只返回一次。我曾嘗試過使用STUFF和CONCAT,但是他們返回的是每個條目相同位置的倍數,而不是查找它們是否具有多個位置並列出它們,這意味着我可能錯誤地使用了這些函數。

下面是電流輸出:

ID   FIRST_NAME   current_flag Position 
---------- -------------------- ------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
106379  Christine   1   NOMAL 
106379  Christine   1   SFC 
106418  Mary     1   CVP 
106751  Denise    1   SDFA 
106885  Marianna    1   RCRA 
107244  Jennifer    1   RCF 

下面是所需的輸出:

ID   FIRST_NAME   current_flag Position 
---------- -------------------- ------------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
106379  Christine   1   NOMAL,SFC 
106418  Mary     1   CVP 
106751  Denise    1   SDFA 
106885  Marianna    1   RCRA 
107244  Jennifer    1   RCF 

我已刪除多餘的欄,因爲輸出是不利於出什麼期望的輸出會是這樣,但是它們包含在我的代碼中,如下所示。 目前代碼:

SELECT DISTINCT Name.ID, 
       Name.FIRST_NAME, 
       Name.LAST_NAME, 
       Name_Address.ADDRESS_1, 
       Name_Address.CITY, 
       Name_Address.STATE_PROVINCE, 
       Name_Address.ZIP, 
       Name_Address.PREFERRED_MAIL,  
       vcsiboCurrentCommittee.current_flag, 
       CONVERT(VARCHAR(10), Name.BIRTH_DATE, 101) AS BirthDate, 
       vcsiboCurrentCommittee.Position 
FROM   Name INNER JOIN 
      Name_Address ON Name.ID = Name_Address.ID INNER JOIN 
      vcsiboCurrentCommittee ON Name.ID = vcsiboCurrentCommittee.ID 
WHERE  (Name_Address.PREFERRED_MAIL = 1.00) 
      AND (vcsiboCurrentCommittee.current_flag = 1.00) 
      AND (Name.ID <> 10) AND (Name.BIRTH_DATE <> '') 
      AND (vcsiboCurrentCommittee.CommitteeCode IN 
     ('ALUMNAE_DEPT', 'COLLEGIATE-DEPT', 'EDUCATION', 'FINANCIAL', 'INTERN_COUNCIL', 'IPDEPT', 'MEMBERSHIP','PANHELLENIC', 'REG_1', 'REG_2', 'REG_3', 'REG_4', 'REG_5', 'REG_6', 'REG_7', 'REG_8')) 

代碼使用STUFF:

SELECT DISTINCT Name.ID, 
       Name.FIRST_NAME, 
       Name.LAST_NAME, 
       Name_Address.ADDRESS_1, 
       Name_Address.CITY, 
       Name_Address.STATE_PROVINCE, 
       Name_Address.ZIP,        
       Name_Address.PREFERRED_MAIL, 
       vcsiboCurrentCommittee.current_flag, 
       CONVERT(VARCHAR(10), Name.BIRTH_DATE, 101) AS BirthDate, 
       STUFF(
       (SELECT 
       CAST(',' AS varchar(max)) + vcsiboCurrentCommittee.Position 
       FROM vcsiboCurrentCommittee AS vcc 
       WHERE vcc.ID = Name.ID 
       ORDER BY vcc.Position 
       FOR xml path('') 
       ), 1, 1, '') AS Positions, 
      vcsiboCurrentCommittee.Position 
FROM Name INNER JOIN 
    Name_Address ON Name.ID = Name_Address.ID INNER JOIN 
    vcsiboCurrentCommittee ON Name.ID = vcsiboCurrentCommittee.ID 
WHERE (Name_Address.PREFERRED_MAIL = 1.00) AND 
     (vcsiboCurrentCommittee.current_flag = 1.00) 
     AND (Name.ID <> 10) AND (Name.BIRTH_DATE <> '') 
     AND (vcsiboCurrentCommittee.CommitteeCode IN ('ALUMNAE_DEPT', 'COLLEGIATE-DEPT', 'EDUCATION', 'FINANCIAL', 'INTERN_COUNCIL', 'IPDEPT', 'MEMBERSHIP', 'PANHELLENIC', 'REG_1', 'REG_2', 'REG_3', 'REG_4', 'REG_5', 'REG_6', 'REG_7', 'REG_8')) 

創建此輸出中:

ID   FIRST_NAME   current_flag Positions                                                              Position 
---------- -------------------- ------------ ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
106379  Christine   1   NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL,NOMAL                                                  NOMAL 
106379  Christine   1   SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC,SFC                                                       SFC 
106418  Mary     1   CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP,CVP                                                    CVP 
106751  Denise    1   SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA,SDFA 

在此先感謝。 SQL Server 2012的

回答

2

我認爲你需要修復子查詢也許select distinct別名和:

STUFF((SELECT DISTINCT CAST(',' AS varchar(max)) + vcc.Position 
---------------------------------------------------^ 
     FROM vcsiboCurrentCommittee AS vcc 
     WHERE vcc.ID = Name.ID 
     ORDER BY vcc.Position 
     FOR xml path('') 
     ), 1, 1, '') AS Positions, 

一旦你解決這個別名(這樣Position指的是內表,而不是外部表)那麼可能不需要DISTINCT。如果您確實使用DISTINCT,則可能還需要調整ORDER BY

編輯:

在外部查詢的重複通過在外部查詢的邏輯創建的。您可能不希望與子查詢中的表相同。很難說出你真正想要的是什麼邏輯。在將其加入其他表之前,可能需要在vcsiboCurrentCommittee上進行某種聚合。

+0

這與我所尋找的結果非常接近。我不得不刪除'ORDER BY'。當前結果仍然顯示每個條目的多行,並且它們列出了多個「職位」。 在評論中格式化不起作用 - 這裏是輸出的截圖[http://imgur.com/TgmKcZN] –