在SQL Server的一行中显示多个行值

时间:2019-01-22 02:24:46

标签: sql sql-server sql-server-2014

此查询:

SELECT DISTINCT
    PT.PRODUCT AS PRODUCT, PT.TEXT_CODE AS TEXT_CODE, 
    PHT.PHRASE AS F_PHRASE
FROM 
    PRODUCT_TEXT PT
LEFT JOIN 
    PHRASE_LINK PHL ON PT.TEXT_CODE = PHL.TEXT_CODE
INNER JOIN 
    PHRASE_TRANSLATIONS PHT ON PHL.PHRASE_ID = PHT.PHRASE_ID
WHERE 
    PT.DATA_CODE = 'MANU' AND PHT.LANGUAGE = 'EN'

UNION ALL

SELECT DISTINCT
    PT.F_ALIAS AS PRODUCT, PT.TEXT_CODE AS TEXT_CODE, 
    PHT.PHRASE AS F_PHRASE
FROM 
    PROD_ALIAS_TEXT PT
LEFT JOIN 
    PHRASE_LINK PHL ON PT.TEXT_CODE = PHL.TEXT_CODE
INNER JOIN 
    PHRASE_TRANSLATIONS PHT ON PHL.PHRASE_ID = PHT.PHRASE_ID
WHERE 
    PT.DATA_CODE = 'MANU' AND PHT.LANGUAGE = 'EN'

返回此输出:

F_PRODUCT       F_TEXTCODE       F_PHRASE 
-----------------------------------------------------
A               MANU001          TEST1      
A               MANU002          TEST2      
B               MANU003          XYZ
C               MANU001          ABC
C               MANU005          DEF

现在,我想将F_PHRASE连接到包含多于一个F_TEXTCODE的产品的一行中。并非适用于所有产品。

采样所需的输出:

F_PRODUCT       F_TEXTCODE              F_PHRASE 
-----------------------------------------------------
A               MANU001,MANU002          TEST1,TEST2      
B               MANU003                  XYZ
C               MANU001,MANU005          ABC,DEF

请提供一些更好的方法来实现这一目标。该表包含10万条记录。

0 个答案:

没有答案