10

我有列的表:

Sr.no  Subject  No of class attended    
-------------------------------------
1       English           3
2       Maths             4
3       SocialScience     5

我想要这种格式的表格

English    Maths   SocialScience
---------------------------------
3            4          5

我试过这个:

Select case when subject ='Maths' then COUNT(No_of_Candidates) else null  end as Maths

但有了这个我得到这样的数据:

 English    Maths   SocialScience
---------------------------------
   3            
               4
                         5

请帮助我应该如何解决这个问题..

4

5 回答 5

7

正如你所说,你不想要这样的输出:

 English    Maths   SocialScience
---------------------------------
   3            
               4
                         5

您需要像这样使用子查询:

SELECT English,Maths,SocialScience
FROM (
SELECT Subject,No_of_class_attended
  FROM mytable) up
PIVOT 
(Sum([No_of_class_attended]) 
 for Subject in ([English],[Maths],[SocialScience])) p

输出:

English    Maths   SocialScience
---------------------------------
3            4          5

  

看到这个 SQLFiddle

有关更多信息,请参阅SQL SERVER – PIVOT 和 UNPIVOT 表示例

于 2012-08-29T11:21:44.083 回答
3

使用枢轴

SELECT *
FROM yourtable
PIVOT 
(Sum([No of class attended]) for Subject in ([English],[Maths],[SocialScience])) p
于 2012-08-29T10:50:59.227 回答
2

podiluska 解决方案是正确的,如果您想将其他主题添加到表中,并且不想更改查询,我只想分享一个动态解决方案。但是它在长度上有一些限制,但是您可以肯定地将它用于某些情况:

DECLARE @SQL nvarchar(MAX)
DECLARE @ColNames nvarchar(max)

SET @ColNames = ''

SELECT @ColNames = (CASE WHEN subjects.Subject IS NOT NULL THEN @ColNames + '[' + subjects.Subject + '],' ELSE '' END)
FROM subjects

SET @ColNames = LEFT(@ColNames, LEN(@ColNames) - 1)


EXEC('SELECT *
FROM subjects
PIVOT 
(Sum([classNum]) for Subject in (' + @ColNames + ')) p')

这是一个SQL Fiddle

于 2012-08-29T11:20:27.513 回答
1

如果你不想使用 PIVOT 关键字,你可以简单地使用 MAX:

Select MAX(case when subject ='Maths' then No_of_Candidates else null  end) as Maths
,MAX(case when subject ='English' then No_of_Candidates else null  end) as English
,MAX(case when subject ='SocialScience' then No_of_Candidates else null  end) as SocialScience
from tableName 

例如,如果表格包含多个学生的结果,则需要使用 GROUP BY,例如:

Select MAX(case when subject ='Maths' then No_of_Candidates else null  end) as Maths
,MAX(case when subject ='English' then No_of_Candidates else null  end) as English
,MAX(case when subject ='SocialScience' then No_of_Candidates else null  end) as SocialScience
from tableName Group By StudentID

任何值都大于 NULL,因此 MAX 将消除 NULL

于 2012-08-29T17:48:08.867 回答
0

所以我遇到了将行转换为列的要求,我想根据数据动态增加列(而不是像英语、数学等那样对它们进行硬编码。你明白了)

这是我的 示例数据

代码:

DECLARE @SQL NVARCHAR(MAX)=''
DECLARE @ColNames NVARCHAR(max)=''

;WITH mycte
AS 
( 
    SELECT *
    ,ROW_NUMBER() OVER ( PARTITION BY SubjectLearnt ORDER BY SubjectLearnt) AS RN
    from STUDENTS
)
SELECT @ColNames = (CASE WHEN mycte.SubjectLearnt IS NOT NULL THEN @ColNames + mycte.SubjectLearnt + ',' ELSE '' END)
FROM mycte
WHERE RN=1
SET @ColNames = LEFT(@ColNames, LEN(@ColNames) - 1)

EXEC('SELECT *
FROM students
PIVOT 
( Sum([subjectMarks]) for SubjectLearnt in (' + @ColNames + ')) p')
于 2018-12-06T14:57:41.203 回答