2

SQL SERVER 2008 R2 を使用しています。複数の値を取得し、同時に行ごとに単一の列に表示したいのですが、値が存在しない場合は、いくつかの値が表示され、単一の列に「12個の値」が表示される可能性があります。

サンプルスクリーンショット 2

たとえば、以下のスクリーン ショットでは、コメントを除いて同じデータのレコードが表示されます。単一のレコードとして表示したいのですが、コメントにはASN 適時性、ASN 精度、問題レポートの両方の値を表示する必要があります。3 つの値は、同じ出荷サイト"0096a"を使用して行ごとに表示されます。

サンプルスクリーンショット 1

私のサンプルクエリ、

SELECT 
    D30.SPGD30_SHIP_SITE_C AS SHIPSITE,  
    D30.SPGD30_RATING_MONTH_Y AS RATINGMONTH,  
    D30.SPGD30_PRIOR_SCORE_R AS PRIOR, 
    D30.SPGD30_REVISED_SCORE_R AS REVISED,  
    CASE WHEN (CHARINDEX('-',D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 ) THEN CONVERT( VARCHAR(8), CAST(D30.SPGD30_TRACKED_ADJUSTMENT_X AS DATETIME) , 1) ELSE D30.SPGD30_TRACKED_ADJUSTMENT_X END ADJUSTMENTS,  
    J02.SPGJ02_MSG_CODE_X AS COMMENTS,  
    D30.SPGD30_LAST_TOUCH_Y AS LASTUPDATED,  
    D30.SPGD30_LAST_TOUCH_C AS LASTUPDATEDCDSID   
FROM  
    CSPGD30_TRACKING D30, 
    CSPGD31_TRACKING_RATING_ELEMNT D31,  
    CSPGA04_RATING_ELEMENT_MSTR A04 , 
    CSPGJ02_MSG_OBJ J02  
WHERE  
    D30.SPGA02_BUSINESS_TYPE_C = D31.SPGA02_BUSINESS_TYPE_C  
AND 
    D30.SPGA03_REGION_C = D31.SPGA03_REGION_C  
AND 
    D30.SPGD30_SHIP_SITE_C = D31.SPGD30_SHIP_SITE_C  
AND 
    D30.SPGD30_RATING_MONTH_Y = D31.SPGD30_RATING_MONTH_Y  
AND 
    D30.SPGD30_TRACKED_ADJUSTMENT_X = D31.SPGD30_TRACKED_ADJUSTMENT_X  
AND 
    D30.SPGD30_LAST_TOUCH_Y = D31.SPGD30_LAST_TOUCH_Y  
AND 
    D31.SPGA04_RATING_ELEMENT_D = A04.SPGA04_RATING_ELEMENT_D  
AND 
    A04.SPGJ02_MSG_K = J02.SPGJ02_MSG_K  
AND 
    D30.SPGA02_BUSINESS_TYPE_C = 'serv'  
AND 
    D30.SPGA03_REGION_C = 'ap'  
AND 
    D30.SPGD30_SHIP_SITE_C = '0134a'  
ORDER BY   
    D30.SPGD30_SHIP_SITE_C ASC  ,
    D30.SPGD30_RATING_MONTH_Y DESC  ,
    D30.SPGD30_LAST_TOUCH_Y DESC 
4

3 に答える 3

7

これを行うには 2 つの方法があります。

まず、相関サブクエリでFOR XML PATHandを使用できます。STUFFこれにより、 の値がCSPGJ02_MSG_OBJ1 つの文字列に連結されます。

SELECT D30.SPGD30_SHIP_SITE_C AS SHIPSITE,  
    D30.SPGD30_RATING_MONTH_Y AS RATINGMONTH,  
    D30.SPGD30_PRIOR_SCORE_R AS PRIOR, 
    D30.SPGD30_REVISED_SCORE_R AS REVISED,  
    CASE WHEN (CHARINDEX('-',D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 ) THEN CONVERT( VARCHAR(8), CAST(D30.SPGD30_TRACKED_ADJUSTMENT_X AS DATETIME) , 1) ELSE D30.SPGD30_TRACKED_ADJUSTMENT_X END ADJUSTMENTS,  
     STUFF((SELECT distinct '+ ' + J02.SPGJ02_MSG_CODE_X
            from CSPGJ02_MSG_OBJ J02
            where A04.SPGJ02_MSG_K = J02.SPGJ02_MSG_K 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,2,'') COMMENTS,
    D30.SPGD30_LAST_TOUCH_Y AS LASTUPDATED,  
    D30.SPGD30_LAST_TOUCH_C AS LASTUPDATEDCDSID   
FROM CSPGD30_TRACKING D30
INNER JOIN CSPGD31_TRACKING_RATING_ELEMNT D31
    ON D30.SPGA02_BUSINESS_TYPE_C = D31.SPGA02_BUSINESS_TYPE_C  
    AND D30.SPGA03_REGION_C = D31.SPGA03_REGION_C  
    AND D30.SPGD30_SHIP_SITE_C = D31.SPGD30_SHIP_SITE_C  
    AND D30.SPGD30_RATING_MONTH_Y = D31.SPGD30_RATING_MONTH_Y  
    AND D30.SPGD30_TRACKED_ADJUSTMENT_X = D31.SPGD30_TRACKED_ADJUSTMENT_X  
    AND D30.SPGD30_LAST_TOUCH_Y = D31.SPGD30_LAST_TOUCH_Y  
INNER JOIN CSPGA04_RATING_ELEMENT_MSTR A04
    ON D31.SPGA04_RATING_ELEMENT_D = A04.SPGA04_RATING_ELEMENT_D  
WHERE D30.SPGA02_BUSINESS_TYPE_C = 'serv'  
    AND D30.SPGA03_REGION_C = 'ap'  
    AND D30.SPGD30_SHIP_SITE_C = '0134a'  
ORDER BY D30.SPGD30_SHIP_SITE_C ASC, D30.SPGD30_RATING_MONTH_Y DESC, D30.SPGD30_LAST_TOUCH_Y DESC;

2 番目の方法は、CROSS APPLYwithを使用することFOR XML PATHです。

SELECT D30.SPGD30_SHIP_SITE_C AS SHIPSITE,  
    D30.SPGD30_RATING_MONTH_Y AS RATINGMONTH,  
    D30.SPGD30_PRIOR_SCORE_R AS PRIOR, 
    D30.SPGD30_REVISED_SCORE_R AS REVISED,  
    CASE WHEN (CHARINDEX('-',D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 ) THEN CONVERT( VARCHAR(8), CAST(D30.SPGD30_TRACKED_ADJUSTMENT_X AS DATETIME) , 1) ELSE D30.SPGD30_TRACKED_ADJUSTMENT_X END ADJUSTMENTS,  
    left(J02.comments, LEN(J02.comments)-1) AS COMMENTS,
    D30.SPGD30_LAST_TOUCH_Y AS LASTUPDATED,  
    D30.SPGD30_LAST_TOUCH_C AS LASTUPDATEDCDSID   
FROM CSPGD30_TRACKING D30
INNER JOIN CSPGD31_TRACKING_RATING_ELEMNT D31
    ON D30.SPGA02_BUSINESS_TYPE_C = D31.SPGA02_BUSINESS_TYPE_C  
    AND D30.SPGA03_REGION_C = D31.SPGA03_REGION_C  
    AND D30.SPGD30_SHIP_SITE_C = D31.SPGD30_SHIP_SITE_C  
    AND D30.SPGD30_RATING_MONTH_Y = D31.SPGD30_RATING_MONTH_Y  
    AND D30.SPGD30_TRACKED_ADJUSTMENT_X = D31.SPGD30_TRACKED_ADJUSTMENT_X  
    AND D30.SPGD30_LAST_TOUCH_Y = D31.SPGD30_LAST_TOUCH_Y  
INNER JOIN CSPGA04_RATING_ELEMENT_MSTR A04
    ON D31.SPGA04_RATING_ELEMENT_D = A04.SPGA04_RATING_ELEMENT_D  
CROSS APPLY
(
    select J02.SPGJ02_MSG_CODE_X  + ', '
    from CSPGJ02_MSG_OBJ J02
    where A04.SPGJ02_MSG_K = J02.SPGJ02_MSG_K  
    FOR XML PATH('')
) J02 (comments)
WHERE D30.SPGA02_BUSINESS_TYPE_C = 'serv'  
    AND D30.SPGA03_REGION_C = 'ap'  
    AND D30.SPGD30_SHIP_SITE_C = '0134a'  
ORDER BY D30.SPGD30_SHIP_SITE_C ASC, D30.SPGD30_RATING_MONTH_Y DESC, D30.SPGD30_LAST_TOUCH_Y DESC;

注: クエリを変更して、句JOINに結合を含むカンマ区切りのテーブルではなく、構文を使用するように変更したことに気付くでしょう。WHEREこれは標準の ANSI 構文です。

于 2013-03-26T14:56:32.487 に答える
0

XML PATH('') の使用

SELECT 
    D30.SPGD30_SHIP_SITE_C AS SHIPSITE,  
    D30.SPGD30_RATING_MONTH_Y AS RATINGMONTH,  
    D30.SPGD30_PRIOR_SCORE_R AS PRIOR, 
    D30.SPGD30_REVISED_SCORE_R AS REVISED,  
    CASE WHEN (CHARINDEX('-',D30.SPGD30_TRACKED_ADJUSTMENT_X) > 0 ) THEN CONVERT( VARCHAR(8), CAST(D30.SPGD30_TRACKED_ADJUSTMENT_X AS DATETIME) , 1) ELSE D30.SPGD30_TRACKED_ADJUSTMENT_X END ADJUSTMENTS,  
    STUFF(JO2.COMMENTS, 1, 1, '') AS COMMENTS,  
    D30.SPGD30_LAST_TOUCH_Y AS LASTUPDATED,  
    D30.SPGD30_LAST_TOUCH_C AS LASTUPDATEDCDSID   
FROM  
    CSPGD30_TRACKING D30 
CROSS JOIN CSPGD31_TRACKING_RATING_ELEMNT D31
CROSS JOIN CSPGA04_RATING_ELEMENT_MSTR A04
CROSS APPLY (
    SELECT
        ',' + ISNULL(JO2.SPGJ02_MSG_CODE_X, '')
    FROM CSPGJ02_MSG_OBJ JO2  
    WHERE A04.SPGJ02_MSG_K = JO2.SPGJ02_MSG_K  
    FOR XML PATH('')
) AS JO2 (COMMENTS)
WHERE  
    D30.SPGA02_BUSINESS_TYPE_C = D31.SPGA02_BUSINESS_TYPE_C  
AND 
    D30.SPGA03_REGION_C = D31.SPGA03_REGION_C  
AND 
    D30.SPGD30_SHIP_SITE_C = D31.SPGD30_SHIP_SITE_C  
AND 
    D30.SPGD30_RATING_MONTH_Y = D31.SPGD30_RATING_MONTH_Y  
AND 
    D30.SPGD30_TRACKED_ADJUSTMENT_X = D31.SPGD30_TRACKED_ADJUSTMENT_X  
AND 
    D30.SPGD30_LAST_TOUCH_Y = D31.SPGD30_LAST_TOUCH_Y  
AND 
    D31.SPGA04_RATING_ELEMENT_D = A04.SPGA04_RATING_ELEMENT_D  
AND 
    D30.SPGA02_BUSINESS_TYPE_C = 'serv'  
AND 
    D30.SPGA03_REGION_C = 'ap'  
AND 
    D30.SPGD30_SHIP_SITE_C = '0134a'  
ORDER BY   
    D30.SPGD30_SHIP_SITE_C ASC  ,
    D30.SPGD30_RATING_MONTH_Y DESC  ,
    D30.SPGD30_LAST_TOUCH_Y DESC 

これはコメントのコンマ区切りリストを作成します。

于 2013-03-25T15:33:14.710 に答える
0

複数の列を 1 つに表示したい場合は、次のように記述できます。

SELECT CAST([MyIntegerId] AS varchar(10)) + ' - ' + [Column1]+ ' - ' +CAST([MyDateTimeColumn] AS varchar(10)) + ' - ' + [Column2] AS 'My Merged Column'
FROM Mytable

複数の列を文字列として単一の列にグループ化する場合は、この記事が役立ちます。

于 2013-04-01T12:55:51.853 に答える