我想基于相同的student_id合并列的内容

时间:2013-01-28 05:33:18

标签: sql-server-2008 merge

我在SQL Server 2008中有一个像这样的表:

st_id   st_rollno  st_name  subject  Theory      Total   Lab      Total
--------------------------------------------------------------------------------
086001  IT001      PRANAV   mat        21           22       11       14
086002  IT002      DEEP     mat        21           22       11       14
086001  IT001      PRANAV   sci        20           24       09       12
086002  IT002      DEEP     sci        21           24       08       12

我希望我的输出如下,使用SQL Server 2008功能,如果有任何1可以帮助我......?

st_id   st_rollno   st_name   subject   Theory   Total            Lab      Total
---------------------------------------------------------------------------------------
086001  IT001      PRANAV   mat,sci 21,20       22,24          11,09       14,12
086002  IT002       DEEP    mat,sci 21,21       22 ,24         11,08       14,12

2 个答案:

答案 0 :(得分:1)

SQL Fiddle

MS SQL Server 2008架构设置

create table YourTable
(
  st_id varchar(6),
  st_rollno varchar(5),
  st_name varchar(6),
  subject varchar(3),
  Theory int,
  Total1 int,
  Lab int,
  Total2 int
)

insert into YourTable values
('086001',  'IT001',      'PRANAV',   'mat',        21,           22,       11,       14),
('086002',  'IT002',      'DEEP  ',   'mat',        21,           22,       11,       14),
('086001',  'IT001',      'PRANAV',   'sci',        20,           24,       09,       12),
('086002',  'IT002',      'DEEP  ',   'sci',        21,           24,       08,       12)

查询1

select T1.st_id,
       T1.st_rollno,
       T1.st_name,
       stuff(T3.X.query('subject').value('.', 'varchar(max)'), 1, 1, '') as subject,
       stuff(T3.X.query('Theory').value('.', 'varchar(max)'), 1, 1, '') as theory,
       stuff(T3.X.query('Total1').value('.', 'varchar(max)'), 1, 1, '') as Total1,
       stuff(T3.X.query('Lab').value('.', 'varchar(max)'), 1, 1, '') as Lab,
       stuff(T3.X.query('Total2').value('.', 'varchar(max)'), 1, 1, '') as Total2
from (
     select st_id, st_rollno, st_name
     from YourTable
     group by st_id, st_rollno, st_name
     ) as T1
cross apply 
     (
     select  ','+T2.subject                     as subject,
             ','+cast(T2.Theory as varchar(10)) as Theory,
             ','+cast(T2.Total1 as varchar(10)) as Total1,
             ','+cast(T2.Lab    as varchar(10)) as Lab,
             ','+cast(T2.Total2 as varchar(10)) as Total2
     from YourTable as T2
     where T1.st_id = T2.st_id and
           T1.st_name = T2.st_name and
           T1.st_rollno = T2.st_rollno
     for xml path(''), type
     ) as T3(X)

<强> Results

|  ST_ID | ST_ROLLNO | ST_NAME | SUBJECT | THEORY | TOTAL1 |  LAB | TOTAL2 |
----------------------------------------------------------------------------
| 086001 |     IT001 |  PRANAV | mat,sci |  21,20 |  22,24 | 11,9 |  14,12 |
| 086002 |     IT002 |  DEEP   | mat,sci |  21,21 |  22,24 | 11,8 |  14,12 |

答案 1 :(得分:0)

从这里改编:SQL Server Concatenate GROUP BY你正在寻找这样的东西:

SELECT 
 st_id, st_rollno, st_name,
 STUFF
 (
    (
        SELECT 
            ',' +subject  
        FROM
            yourtable
        WHERE
            yourtable.st_id=st_id
        FOR XML PATH('')
    )
,1,1,'') AS subject, 

...

FROM yourtable

这里要理解的核心概念是stufffor xml我没有正确的语法,但如果没有其他人发帖,这应该会让你走上正确的道路。当我靠近SSMS并且可以测试时,我会更新这个。绝对这是可行的。我发现使用Group_Concat在MySQL中更容易。