2015-10-23 68 views
0

我有一個表,看起來像如下因素多個ID值單值

ID  LINE_NUMBER CODE   DATA 
39311 1   AXFP-20090 A 
39311 2   AXFP-20227 B 
39311 3   AXFP-20067 B 
39311 4   AXFP-20337 B 
39311 5   AXFP-20081 B 
39311 6   AXFP-20048 B 
39311 7   AXFP-20025 B 
39311 8   AXFP-22371 B 
39311 9   AXFP-20176 B 
39311 10   AXFP-20278 B 
39311 11   AXFP-21838 B 
39311 14   AXFP-92035 C 
41681 1   AXFP-99999 A 
41681 2   AXFP-58229 A 
41681 3   AXFP-51166 A 
41681 5   AXFP-21848 C 
41681 6   AXFP-58191 C 
41681 7   AXFP-61481 C 
41681 8   AXFP-21757 A 
41681 9   AXFP-32704 C 
41681 10   AXFP-60037 B 
41681 11   AXFP-21849 B 
41681 12   AXFP-34021 C 
41681 13   AXFP-34072 C 

我需要的數據,每個ID合併成一排。持有以下格式

enter image description here

任何人有任何想法如何,我能做到這一點的行號,代碼和數據?

+0

你需要一組CONCAT:http://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server –

回答

0
select ID  , 
Data=Stuff((SELECT ' ' + CAST(LINE_NUMBER as VARCHAR(255)) + ')' + CODE + ' ' + DATA FROM t t1 WHERE t1.ID =t.ID   
FOR XML PATH ('')) 
      , 1, 1, '') 
from t 
GROUP BY ID 

FIDDLE