Oracle decode case when 行转列 分组查询

本文介绍如何使用SQL进行行转列查询,并通过decode、casewhen和子查询实现数据聚合。此外,展示了如何利用casewhen判断不同年龄段的学生人数,并通过嵌套casewhen简化SQL查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.行转列查询,使用 decode或者case when
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql语句?
胜 负
2005-05-09 2 2
2005-05-10 1 2

--生成表和数据
create table Test_Decode(rq varchar(10),shengfu nchar(1));
insert into Test_Decode values('2005-05-09','胜');
insert into Test_Decode values('2005-05-09','胜');
insert into Test_Decode values('2005-05-09','负');
insert into Test_Decode values('2005-05-09','负');
insert into Test_Decode values('2005-05-10','胜');
insert into Test_Decode values('2005-05-10','负');
insert into Test_Decode values('2005-05-10','负');

--1 使用 decode 实现
--1.1 查看数据
select rq,shengfu from test_decode;
--1.2 进一步分析数据
select rq,decode(shengfu,'胜',1,0) sheng,decode(shengfu,'负',1,0) fu from test_decode;
--1.3 最后得到想要的数据
select rq,sum(decode(shengfu,'胜',1,0)) 胜,sum(decode(shengfu,'负',1,0)) 负 from test_decode group by rq order by 1;

--2 使用 case when 实现
--2.1 方法1 case when shengfu='胜'
select rq,
       sum(case when shengfu='胜' then 1 else 0 end) 胜 ,
       sum(case when shengfu='负' then 1 else 0 end) 负 
       from test_decode group by rq order by 1;

--2.2 方法2 case shengfu when to_nchar('胜')
select rq,
       sum(case shengfu when to_nchar('胜') then 1 else 0 end) 胜 ,
       sum(case shengfu when to_nchar('负') then 1 else 0 end) 负 
       from test_decode group by rq order by 1;

--3 使用子查询
select tab1.rq,tab1.胜,tab2.负 from 
       ( select rq,count(*) 胜 from Test_Decode where shengfu = to_nchar('胜') group by rq ) tab1 join
       ( select rq,count(*) 负 from Test_Decode where shengfu = to_nchar('负') group by rq ) tab2 on (tab1.rq = tab2.rq)
       order by 1;

2.查出不同年龄段的学生的人数
create table student(
       id number(4) primary key,
       name varchar2(90),
       age number(4)
);

select id,name,age from student;
insert into student values (1,'a',10);
insert into student values (2,'b',20);
insert into student values (3,'c',30);
insert into student values (4,'d',40);
insert into student values (5,'e',50);
insert into student values (6,'f',60);
insert into student values (7,'g',70);
insert into student values (8,'h',80);
insert into student values (9,'i',90);
insert into student values (10,'g',100);
insert into student values (11,'g',66);
insert into student values (12,'g',77);
insert into student values (13,'g',55);
--查询出这样的数据
年龄段,人数
   < 30  数目
30 - 60  数目
60 >     数目

select name,age from student;
--使用两次case when判断,group by也要使用case when
select ( case when age <= 30 then '   < 30'
            when age > 30 and age <= 60 then '30 - 60'
            when age > 60 then '60 > '
       end ) 年龄段,
       sum( case when age <= 30 then 1
            when age > 30 and age <= 60 then 1
            when age > 60 then 1
       end ) 人数
       from student 
       group by 
           case when age <= 30 then '   < 30'
                when age > 30 and age <= 60 then '30 - 60'
                when age > 60 then '60 > '
           end ;

--加一层嵌套使SQL更加简洁
select age_area 年龄段,sum(age_num) 人数 from (
    select  case when age <= 30 then '   < 30'
                when age > 30 and age <= 60 then '30 - 60'
                when age > 60 then '60 > '
           end  age_area,
           case when age <= 30 then 1
                when age > 30 and age <= 60 then 1
                when age > 60 then 1
           end age_num
           from student 
) group by age_area;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值