在SQL中将行转换为列

时间:2022-12-27 23:42:54

I'm beginner in SQl,how can i do this in that ? is this possible ?

我是SQl的初学者,我怎么能这样做呢?这可能吗 ?

在SQL中将行转换为列

1 个解决方案

#1


3  

You can use the PIVOT table operator to do transform the rows of these course's grades into column, like so:

您可以使用PIVOT表运算符将这些课程成绩的行转换为列,如下所示:

SELECT  
  Course, 
  [1] AS "Grade 1", 
  [2] AS "Grade 2", 
  [3] AS "Grade 3", 
  [4] AS "Grade 4"
FROM
(
  SELECT 
    Course, 
    Grade, 
    ROW_NUMBER() OVER(PARTITION BY Course 
                      ORDER BY COURSE) rownum 
  FROM Grades
) t
PIVOT 
(
   MAX(Grade)
   FOR rownum IN([1], [2], [3], [4])
) p;

SQL Fiddle Demo

This will give you:

这会给你:

COURSE    GRADE 1   GRADE 2      GRADE 3     GRADE 4
  A         15        17          (null)     (null)
  B         12      (null)        (null)     (null)
  C         10        18            0           9

Note that: I used the ranking function ROW_NUMBER() with PARTITION BY Course to partition them into four grades. So that you could then group them into these four groups.

请注意:我使用PARTITION BY课程的排名函数ROW_NUMBER()将它们分成四个等级。这样你就可以将它们分成这四组。

If you need to this dynamically, in case there was unknown number of grades. You can use dynamic SQL in this case to generate the list of grades dynamically like so:

如果你需要动态,如果有不明数量的成绩。在这种情况下,您可以使用动态SQL动态生成成绩列表,如下所示:

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

SELECT @cols = STUFF((SELECT distinct ',' 
                        + QUOTENAME('Grade ' 
                        + CAST(ROW_NUMBER() OVER(PARTITION BY Course 
                                               ORDER BY Course) AS VARCHAR(10)))
                FROM Grades
                         FOR XML PATH(''), TYPE
                      ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');

SELECT @query = 'SELECT Course, ' + @cols +  
                'FROM
                 (
                   SELECT 
                     Course, 
                     Grade, 
                     ''Grade '' + CAST(ROW_NUMBER() 
                                       OVER(PARTITION BY Course 
                                ORDER BY COURSE) AS VARCHAR(10)) rownum 
                   FROM Grades
                 ) t
                 PIVOT 
                 (
                   MAX(Grade)
                   FOR rownum IN(' + @cols + ')' +
                ') p';
execute(@query);

Dynamic SQL Fiddle Demo

#1


3  

You can use the PIVOT table operator to do transform the rows of these course's grades into column, like so:

您可以使用PIVOT表运算符将这些课程成绩的行转换为列,如下所示:

SELECT  
  Course, 
  [1] AS "Grade 1", 
  [2] AS "Grade 2", 
  [3] AS "Grade 3", 
  [4] AS "Grade 4"
FROM
(
  SELECT 
    Course, 
    Grade, 
    ROW_NUMBER() OVER(PARTITION BY Course 
                      ORDER BY COURSE) rownum 
  FROM Grades
) t
PIVOT 
(
   MAX(Grade)
   FOR rownum IN([1], [2], [3], [4])
) p;

SQL Fiddle Demo

This will give you:

这会给你:

COURSE    GRADE 1   GRADE 2      GRADE 3     GRADE 4
  A         15        17          (null)     (null)
  B         12      (null)        (null)     (null)
  C         10        18            0           9

Note that: I used the ranking function ROW_NUMBER() with PARTITION BY Course to partition them into four grades. So that you could then group them into these four groups.

请注意:我使用PARTITION BY课程的排名函数ROW_NUMBER()将它们分成四个等级。这样你就可以将它们分成这四组。

If you need to this dynamically, in case there was unknown number of grades. You can use dynamic SQL in this case to generate the list of grades dynamically like so:

如果你需要动态,如果有不明数量的成绩。在这种情况下,您可以使用动态SQL动态生成成绩列表,如下所示:

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

SELECT @cols = STUFF((SELECT distinct ',' 
                        + QUOTENAME('Grade ' 
                        + CAST(ROW_NUMBER() OVER(PARTITION BY Course 
                                               ORDER BY Course) AS VARCHAR(10)))
                FROM Grades
                         FOR XML PATH(''), TYPE
                      ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');

SELECT @query = 'SELECT Course, ' + @cols +  
                'FROM
                 (
                   SELECT 
                     Course, 
                     Grade, 
                     ''Grade '' + CAST(ROW_NUMBER() 
                                       OVER(PARTITION BY Course 
                                ORDER BY COURSE) AS VARCHAR(10)) rownum 
                   FROM Grades
                 ) t
                 PIVOT 
                 (
                   MAX(Grade)
                   FOR rownum IN(' + @cols + ')' +
                ') p';
execute(@query);

Dynamic SQL Fiddle Demo