2

我正在使用SQL SERVER 2008 R2。我想要獲取多個值,並在同一行中逐行顯示它,如果值存在,則顯示一些值,並且可能是單列上的'12值'的可能性。如何在SQL SERVER 2008 R2中逐行顯示多個值?

Sample screenshot 2

對於實施例,下面的屏幕截圖,它示出了用於除了註釋相同的數據記錄。我想顯示爲單個記錄,意見應顯示兩個值是ASN及時性,準確性ASN,問題報告。這三個值使用相同的船廠「0096a」逐行顯示。

Sample screenshot 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 
+0

你想要的結果是什麼?有很多列,你想展示哪些列? – 2013-02-18 09:39:23

+0

我想在單個列中顯示多個值(12個評分元素)。例如,如果存在8個評級要素,我們在單個列中只顯示8個評級要素。 – 2013-02-18 09:42:36

+2

你應該看看這個:[http://stackoverflow.com/questions/1574407/how-to-concatenate-n-columns-into-one](http://stackoverflow.com/questions/1574407/how-to -concatenate-n-columns-into-one) – Dalex 2013-02-18 15:45:15

回答

0

使用

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 

這人會做一個逗號分隔的評論列表XML PATH( '')。

+0

這不是老闆的工作。 – 2013-03-26 13:13:46

+0

我寫了一個0而不是一個O.很高興有人給我一個這樣的投票...(是的,我猜這個有趣的部分不是CROSS APPLY或XML PATH,而是Oes和Zeroes .. ) – Serge 2013-03-26 15:16:16

+4

我沒有投票,但你能解釋爲什麼你在WHERE子句中使用CROSS JOIN和連接條件,而不是適當的,現代的和更直觀的INNER JOIN語法? – 2013-03-26 15:38:00

7

我看到兩種方法可以做到這一點。

首先,您可以在相關子查詢中使用FOR XML PATHSTUFF。這將串連從CSPGJ02_MSG_OBJ值成一個字符串:

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; 

第二種方法是使用一個CROSS APPLYFOR 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語法。

+0

它不適合我。它不逐行顯示。所以我能做些什麼。它顯示了兩條記錄(使用SQL Server 2008 R2) – 2013-03-27 13:27:06

+0

@Adalarasan_Serangulam我的建議是用你的表和一些示例數據創建一個SQL小提琴或用每個表的完整表結構和示例數據編輯你的文章。 – Taryn 2013-03-27 13:49:01

+0

好吧,我會盡我最好的謝謝 – 2013-03-27 14:31:25

0

如果你想在一個顯示多列,你可以這樣寫:

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

如果你想通過多列單列爲一個字符串組 - 這article應該可以幫助您:

相關問題