-----decode行转列
select
name
"姓名"
,
sum
(decode(subject,
‘语文‘
, nvl(score, 0), 0))
"语文"
,
sum
(decode(subject,
‘数学‘
, nvl(score, 0), 0))
"数学"
,
sum
(decode(subject,
‘英语‘
, nvl(score, 0), 0))
"英语"
from
student_score
group
by
name
;
------ case when 行转列
select
name
"姓名"
,
sum
(
case
when
subject=
‘语文‘
then
nvl(score,0)
else
0
end
)
"语文"
,
sum
(
case
when
subject=
‘数学‘
then
nvl(score,0)
else
0
end
)
"数学"
,
sum
(
case
when
subject=
‘英语‘
then
nvl(score,0)
else
0
end
)
"英语"
from
student_score
group
by
name
;
时间: 2024-10-11 04:12:04