sql中列数据横着显示


列数据横着显示:
CREATE TABLE StudenScore(stuname VARCHAR(25) , kc VARCHAR(25) , fs INT)
INSERT INTO StudenScore VALUES(‘张三‘ , ‘语文‘ , 74);
INSERT INTO StudenScore VALUES(‘张三‘ , ‘数学‘ , 83);
INSERT INTO StudenScore VALUES(‘张三‘ , ‘物理‘ , 93);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘语文‘ , 74);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘数学‘ , 84);
INSERT INTO StudenScore VALUES(‘李四‘ , ‘物理‘ , 94)

SELECT * FROM StudenScore

SELECT stuname AS 姓名 ,
MAX(CASE kc WHEN ‘语文‘ THEN fs ELSE 0 END) 语文,
MAX(CASE kc WHEN ‘数学‘ THEN fs ELSE 0 END) 数学,
MAX(CASE kc WHEN ‘物理‘ THEN fs ELSE 0 END) 物理
FROM StudenScore
GROUP BY stuname;

 

SELECT stuname AS 姓名 ,
SUM(CASE kc WHEN ‘语文‘ THEN fs ELSE 0 END) 语文,
SUM(CASE kc WHEN ‘数学‘ THEN fs ELSE 0 END) 数学,
SUM(CASE kc WHEN ‘物理‘ THEN fs ELSE 0 END) 物理
FROM StudenScore
GROUP BY stuname;

 

至于为MAX和SUM在此处两者没有区别,都能查处数据

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。