标签:
列数据横着显示:
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在此处两者没有区别,都能查处数据
标签:
原文地址:http://www.cnblogs.com/bky-234/p/4444124.html