【Oracle】行转列

defects表结构:

create table defects(
    id number(4),
    code varchar2(6),
    type varchar2(10) check(type='oper' or type='sql' or type='api'),
    status number(1) check(status=0 or status=1),
    remark nvarchar2(100),
    primary key(id)
)

 

插值:

复制代码
declare 
   d integer;
   status integer;
begin
for i in 1..1000 loop
    d:=dbms_random.value(1,3);
    status:=dbms_random.value(0,1);

    insert into defects values(i,
                               dbms_random.string('*',6),
                               decode(d,1,'oper',2,'sql',3,'api'),
                               status,
                   '');
end loop;

commit;

end;
/
复制代码

// 正常查询

select 
    type,
    count(*) as total,
    sum(decode(status,1,1,0,0)) as completed,
    sum(decode(status,1,0,0,1)) as awiating
from defects
group by type

效果:

复制代码
SQL> select
  2      type,
  3      count(*) as total,
  4      sum(decode(status,1,1,0,0)) as completed,
  5      sum(decode(status,1,0,0,1)) as awiating
  6  from defects
  7  group by type;

TYPE                      TOTAL  COMPLETED   AWIATING
-------------------- ---------- ---------- ----------
api                         253        126        127
sql                         511        248        263
oper                        236        112        124
复制代码

 

行转列:

语句:

复制代码
select
    'total' as class,
    sum(case type when 'oper' then 1 end) as oper,
    sum(case type when 'sql' then 1 end) as sql,
    sum(case type when 'api' then 1 end) as api
from defects
union all
select
    'completed' as class,
    sum(case type when 'oper' then status end) as oper,
    sum(case type when 'sql' then status end) as sql,
    sum(case type when 'api' then status end) as api
from defects
union all
select
    'awiating' as class,
    sum(case type when 'oper' then decode(status,1,0,0,1) end) as oper,
    sum(case type when 'sql' then decode(status,1,0,0,1) end) as sql,
    sum(case type when 'api' then decode(status,1,0,0,1) end) as api
from defects
复制代码

效果:

复制代码
SQL> select
  2      'total' as class,
  3      sum(case type when 'oper' then 1 end) as oper,
  4      sum(case type when 'sql' then 1 end) as sql,
  5      sum(case type when 'api' then 1 end) as api
  6  from defects
  7  union all
  8  select
  9      'completed' as class,
 10      sum(case type when 'oper' then status end) as oper,
 11      sum(case type when 'sql' then status end) as sql,
 12      sum(case type when 'api' then status end) as api
 13  from defects
 14  union all
 15  select
 16      'awiating' as class,
 17      sum(case type when 'oper' then decode(status,1,0,0,1) end) as oper,
 18      sum(case type when 'sql' then decode(status,1,0,0,1) end) as sql,
 19      sum(case type when 'api' then decode(status,1,0,0,1) end) as api
 20  from defects;

CLASS                    OPER        SQL        API
------------------ ---------- ---------- ----------
total                     236        511        253
completed                 112        248        126
awiating                  124        263        127
复制代码

 

END

posted @   逆火狂飙  阅读(91)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2019-10-19 中划线下划线在英语里怎么读?
2013-10-19 截短字符串的函数(JS中适用)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示