2015-08-14 70 views
0

合併單元格的數據,我有一個表:使用SQL Server

Student| Grade 
qwe | 100 
qwe | 95 
qwe | 90 
asd | 85 
asd | 90 
zxc | 80 
zxc | 75 
zxc | 100 

和我這樣的預期的輸出,我怎麼能這樣做?

Student | Grade 
qwe  | 100; 95; 90 
asd  | 85; 90 
zxc  | 80; 75; 100 

我想這樣的,但它是錯誤的:

SELECT d.Student, d.Grade FROM Data d GROUP BY d.Student 

請幫助。

感謝大家。我用這個查詢和它的作品:

SELECT DISTINCT t1.Student, 
(SELECT t2.Grade+'; ' 
    FROM Data t2 
    WHERE t2.Student = t1.Student 
    FOR XML PATH('')) Concatenated 
FROM Data t1 

回答

0

我複製這從JPW的鏈接:

SELECT STUFF(
      (SELECT ',' + Column_Name 
       FROM Table_Name as [InteriorQuery] 
       FOR XML PATH ('')) 
      , 1, 1, '') 
FROM TABLE_NAME as [ExteriorQuery] 

裏面的東西,放置一個where子句,如

where [InteriorQuery].Student = ExteriorQuery.Student