3

如何更改此表

Name      subject     Mark
Aswin     physics     100
Aswin     chemistry   300
Aswin     maths       200

进入

Aswin Physics 100 Chemistry 300 Maths 200

任何人请帮助我。

4

4 回答 4

2

您可以使用 PIVOT 运算符在 sql server 中完成这项工作。

检查这些链接link1link2他们将展示如何将行更改为列。

希望这对你有帮助!

于 2013-02-15T07:02:25.980 回答
2

SQLFiddle 演示

select Name,
       sum(CASE 
          when [subject]='physics' then Mark
       end) as Physics,
       sum(CASE 
          when [subject]='chemistry' then Mark
       end) as chemistry,
       sum(CASE 
          when [subject]='maths' then Mark
       end) as maths


from t group by Name

或者,如果您需要它在一行中:

SQLFiddle 演示

SELECT
   t1.name,
   MemberList = substring((SELECT ( ', ' + subject+' - '+
                                   cast(Mark as varchar(100)) )
                           FROM t t2
                           WHERE t1.name = t2.name
                           ORDER BY 
                              name,
                              subject
                           FOR XML PATH( '' )
                          ), 3, 1000 )FROM t t1
GROUP BY name
于 2013-02-15T07:04:04.600 回答
1

如果您希望将这些数据放在单独的列中还是在一列中,尚不清楚。

如果您希望在单独的列中使用此功能,则可以应用PIVOTSQL Server 2005 中提供的功能。

如果您知道要转换的所有值或数量有限,则可以对查询进行硬编码:

select *
from
(
  select name, subject +' '+ cast(mark as varchar(9)) as sub_mark,
    'Subject_'+cast(row_number() over(partition by name 
                                     order by subject) as varchar(10)) col_name
  from subjects
) s
pivot
(
  max(sub_mark)
  for col_name in (Subject_1, Subject_2, Subject_3)
) piv;

请参阅SQL Fiddle with Demo您会注意到,我这样做与其他枢轴答案略有不同。我将主题/标记放在同一列中,列名称为Subject_1等。

如果您有未知数量的值,则可以使用动态 sql:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT distinct ',' + QUOTENAME('Subject_'+cast(row_number() over(partition by name 
                                     order by subject) as varchar(10))) 
                    from subjects
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT name,' + @cols + ' from 
             (
                select name, subject +'' ''+ cast(mark as varchar(9)) as sub_mark,
                  ''Subject_''+cast(row_number() over(partition by name 
                                                   order by subject) as varchar(10)) col_name
                from subjects
            ) x
            pivot 
            (
              max(sub_mark)
              for col_name in (' + @cols + ')
            ) p '

execute(@query)

请参阅SQL Fiddle with Demoname如果 a有超过 3 个主题,动态 sql 版本将增加列数。

两个查询的结果是:

|  NAME |     SUBJECT_1 | SUBJECT_2 |   SUBJECT_3 |
---------------------------------------------------
| Aswin | chemistry 300 | maths 200 | physics 100 |
于 2013-02-15T11:06:27.807 回答
1

您需要使用 SQL Pivoting,查看SQL SERVER – PIVOT 和 UNPIVOT 表示例中的示例。使用 Sql Pivoting 您可以将行更改为列,而 Unpivoting 用于列到行的转换。

请注意:我正在检查是否可以为您提供确切的脚本,但现在该链接可以帮助您。

更新

代码示例
虽然我没有用实际数据对此进行测试,但它解析得很好。

-- Pivot Table ordered by Name of Student
SELECT Name, Physics, Chemistry, Maths
FROM (
SELECT Name, Subject, Mark
FROM Student) up
PIVOT (SUM(Mark) FOR Student IN (Physics, Chemistry, Maths)) AS pvt
ORDER BY Name

-- Result should be something like
----------------------------------
Name   Physics   Chemistry   Maths
----------------------------------
Aswin  100       300         200

----------------------------------

要创建数据透视,您需要知道要转换为列的实际行值。如果你觉得它有用的话,我之前在这里写过关于动态旋转的文章。

于 2013-02-15T07:00:22.370 回答