-- sql行轉列
create table student_scores(
username varchar(10),-- 學生姓名
subject varchar(10),-- 科目名稱
score int(3)-- 成績
)insert into student_scores values('張三','語文',86);
insert into student_scores values('張三','數學',96);
insert into student_scores values('張三','英語',76);
insert into student_scores values('李四','語文',59);
insert into student_scores values('李四','數學',61);
insert into student_scores values('李四','英語',88);
-- 行轉列
select username,
max(case subject when '語文' then score end) as '語文',
max(case subject when '數學' then score end) as '數學',
max(case subject when '英語' then score end) as '英語'
from student_scores
group by username
union
select username,sum(chinese),sum(math),sum(english) from(
select '單科總成績' username,
sum(case subject when '語文' then score end) as chinese,
sum(case subject when '數學' then score end) as math,
sum(case subject when '英語' then score end) as english
from student_scores
group by username )t
group by username
sql 行轉列 思想及示例
sql 行轉列常常在具體的業務需要中出現難於處理的情況,使用開發難度增大。故再回顧下行轉列的思想與示例。以下是詳細sql指令碼 if exists in column nvarchar 500 要把資料轉成列的列名 as begin declare queryfiled nvarchar max 用...
sql 行轉列 思想及示例
sql 行轉列常常在具體的業務需要中出現難於處理的情況,使用開發難度增大。故再回顧下行轉列的思想與示例。以下是詳細sql指令碼 if exists in column nvarchar 500 要把資料轉成列的列名 as begin declare queryfiled nvarchar max 用...
MySQL行轉列sql語句
create table test tb grade id int 10 notnull auto increment user name varchar 20 default null course varchar 20 default null score float default 0 pri...