我正在使用 SQL SERVER 2008 R2。我想获取多个值并同时逐行显示在单列中,如果存在值,否则它会显示一些值,并且可能在单列上显示“12 个值”。
例如,下面的屏幕截图,它显示了相同数据的记录,但评论除外。我想显示为单个记录,但注释应显示ASN Timeliness、ASN Accuracy、问题报告这两个值。这三个值使用同一船厂“0096a”逐行显示。
我的示例查询,
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