我的Sql数据输出将行转换为像pivot表一样的列

时间:2016-11-08 12:26:33

标签: mysql

我有一张叫做标记的桌子。我的表中的样本数据如下。在我的表格中,主题字段是动态的,主题的名称和数量可能因类别而异。

  ID        Name        Subject     Marks
------    ---------    ----------  ---------       
1001        John          Maths       78
1001        John          English     88
1001        John          Computer    92
1002        Mary          Maths       81
1002        Mary         English      85
1002        Mary         Computer     90

如何以下面的格式获得上表数据的输出。我不知道这是一个简单的问题,我是SQL和开发领域的初学者。请帮忙。

ID    Name         Maths        English    Computer
---- --------     ---------   ---------    ------------      
1001  John          78             88           92
1002  Mary          81             85           90 

3 个答案:

答案 0 :(得分:1)

更简单的解决方案是使用条件聚合,如此

Select id,name,
sum(case when s.SUBJECT='Computer' then s.marks else 0 end) as Computer,
sum(case when s.SUBJECT='English' then s.marks else 0 end) as English,
sum(case when s.SUBJECT='Maths' then s.marks else 0 end) as Maths 
from marks s group by id,name;

如果您不知道有多少科目,那么您必须创建一个sql语句并运行它(动态sql)。 我在简单的阶段做到这一点 所以

set @sumstr = 
(select str from
(
select @rn:=@rn + 1 rn,@str:=concat(@str,'sum(case when s.SUBJECT=',char(39),s.SUBJECT,char(39),' then s.marks else 0 end) as ', s.SUBJECT, ',' ) str
from (select @rn:=0,@str:='') str,(SELECT DISTINCT SUBJECT FROM MARKS) s
ORDER BY S.SUBJECT
) s
order by rn desc limit 1
);

创建sum语句,然后我就这样充实

SET @SQLSTR = concat(
                'Select id,name,'
                ,SUBSTRING(@SUMSTR,1,LENGTH(@SUMSTR) -1)
                ,' from marks s group by id,name;'
                )
;

在此解决方案的顶部创建sql语句。 然后我准备并执行它

prepare dynamic_statement from @sqlstr;
execute dynamic_statement;

答案 1 :(得分:0)

您可以使用以下查询

SELECT ID, 
       Name,
       CAST(SUBSTRING_INDEX(Marks, ',', 1)  AS UNSIGNED) Maths, 
       CAST(SUBSTRING_INDEX(Marks, ',', 2) AS UNSIGNED) English, 
       CAST(SUBSTRING_INDEX(Marks, ',', 3) AS UNSIGNED) Computer 
FROM  (SELECT ID, 
              Name,
              GROUP_CONCAT(CAST(Marks AS VARCHAR(100))) Marks
       FROM marks
       GROUP  BY ID, 
              Name) derived_table2

希望这可以解决你的问题。

答案 2 :(得分:0)

you can also try like..


select id,name,sum(Maths)Maths,sum(English)English,sum(computer)computer
from(
select id,name,
case when Subject='Maths' then Marks else 0 end Maths,
case when Subject='English' then Marks else 0 end English,
case when Subject='computer' then Marks else 0 end computer
from(
select * from your_table_name
)A
 )B group by id