--场景1:
A  B
a  1
a  2
a  3
b  4
b  5 希望实现如下效果:
a 1,2,3
b 4,5 create table tmp as
select 'a' A, 1 B from dual union all
select 'a' A, 2 B from dual union all
select 'a' A, 3 B from dual union all
select 'b' A, 4 B from dual union all
select 'b' A, 5 B from dual; 1.方法1:listagg
--listagg() + group by: 推荐使用
select a,listagg(b,',') within group (order by b) as c from tmp group by a;
--listagg() + over(partition by )
select distinct a,listagg(b,',') within group (order by b) over(partition by a) as c from tmp ; 2.wm_concat
select a,to_char(wm_concat(b)) as b from tmp group by a 3.sys_connect_by_path
select a, max(substr(sys_connect_by_path(b, ','), 2)) str
from (select a, b, row_number() over(partition by a order by b) rn from tmp)
start with rn = 1
connect by rn = prior rn + 1
and a = prior a
group by a; 4.max+decode
select a,
max(decode(rn, 1, b, null)) ||
max(decode(rn, 2, ',' || b, null)) ||
max(decode(rn, 3, ',' || b, null)) str
from (select a,b,row_number() over(partition by a order by b) as rn from tmp)
group by a
order by 1; 5.row_number()+lead
select a, str b
from (select a,
row_number() over(partition by a order by b) as rn,
b || lead(',' || b, 1) over(partition by a order by b) ||
lead(',' || b, 2) over(partition by a order by b) ||
lead(',' || b, 3) over(partition by a order by b) as str
from tmp
)
where rn = 1
order by 1; 6.model语句
select a, substr(str,2) b
from tmp
model return updated rows partition by(a) dimension by(row_number() over(partition by a order by b) as rn)
measures(cast(b as varchar2(20)) as str)
rules upsert iterate(3) until(presentv(str[iteration_number+2],1,0) = 0)
(str[] = str[]||','||str[iteration_number + 1])
order by 1; --场景2:
no sex
004 2
002 2
002 2
003 1
002 1 希望实现如下效果:
c1 c2
002 1 2
003 1 0
004 0 1 也就是说按no sex两个字段count人数,得到二维表。 --1.添加测试数据
create table tt(no varchar(20 char), sex number);
insert into tt values('',2);
insert into tt values('',2);
insert into tt values('',2);
insert into tt values('',1);
insert into tt values('',1);
commit;
select * from tt; --2.SQL实现
--存储过程动态拼接
--(1)使用case
create or replace procedure row_to_line
is
str_sql varchar2(4000);
begin
str_sql := ' create or replace view v_row_to_line as select no '; for x in (select distinct sex from tt) loop
str_sql := str_sql || ',count(case when sex = '||x.sex||' then 1 else null end ) "'||x.sex||'"';
end loop; str_sql := str_sql || ' from tt group by no order by no '; execute immediate str_sql; end;
/ --(2)使用decode
create or replace procedure row_to_line
is
str_sql varchar2(4000);
begin
str_sql := ' create or replace view v_row_to_line as select no '; for x in (select distinct sex from tt) loop
str_sql := str_sql || ',count(decode(sex, '||x.sex||', 1 , null)) "'||x.sex||'"';
end loop; str_sql := str_sql || ' from tt group by no order by no '; execute immediate str_sql; end;
/ SQL> exec row_to_line; PL/SQL procedure successfully completed SQL> select * from v_row_to_line; NO 1 2
---------------------------------------- ---------- ----------
002 1 2
003 1 0
004 0 1 --(3)动态传表名+列名
create or replace procedure row_to_line
(
str_tabname in varchar2,
str_col1 in varchar2,
i_col2 in varchar2
)
is
str_sql varchar2(4000);
begin
str_sql := ' create or replace view v_row_to_line as select '||str_col1||' '; for x in (select distinct sex from tt ) loop
str_sql := str_sql || ',count(decode('||i_col2||', '||x.sex||', 1, null)) "'||x.sex||'"';
end loop; str_sql := str_sql || ' from '||str_tabname||' group by '||str_col1||' order by '||str_col1||' '; execute immediate str_sql; end; --(4)使用游标
create or replace procedure row_to_line
(
str_tabname in varchar2,
str_col1 in varchar2,
i_col2 in varchar2,
cur_result out sys_refcursor
)
is
str_sql varchar2(4000);
begin
str_sql := 'select '||str_col1||' '; for x in (select distinct sex from tt ) loop
str_sql := str_sql || ',count(decode('||i_col2||', '||x.sex||', 1, null)) "'||x.sex||'"';
end loop; str_sql := str_sql || ' from '||str_tabname||' group by '||str_col1||' order by '||str_col1||' '; open cur_result for str_sql; end; --(5).使用sql语句也可以解决
select no,
count(case sex when 1 then 1 else null end) c1,
count(case sex when 2 then 1 else null end) c2
from tt
group by no
order by no;

Oracle中的行转列例子详解的更多相关文章

  1. oracle中的行转列,列转行

    行转列:源表: 方法1:case when select y,sum(case when q=1 then amt end) q1,sum(case when q=2 then amt end) q2 ...

  2. oracle中的exists 和 in 用法详解

    以前一直不知道exists和in的用法与效率,这次的项目中需要用到,所以自己研究了一下.下面是我举两个例子说明两者之间的效率问题. 前言概述: “exists”和“in”的效率问题,涉及到效率问题也就 ...

  3. oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决

    --Type CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT ( CURR_STR clob, STATIC FUN ...

  4. c语言中命令行参数argc,argv[]详解

    main(int argc,char *argv[ ]) 1.argc为整数 2.argv为指针的指针(可理解为:char **argv or: char *argv[] or: char argv[ ...

  5. mysql 中实现行变列

    前言: mysql行列变化,最难的就是将多个列变成多行,使用的比较多的是统计学中行变列,列变行,没有找到现成的函数或者语句,所以自己写了存储过程,使用动态sql来实现,应用业务场景,用户每个月都有使用 ...

  6. sql中的行转列和列转行的问题

    sql中的行转列和列转行的问题 这是一个常见的问题,也是一个考的问题 1.行转列的问题  简单实例 CREATE TABLE #T ( MON1 INT, MON2 INT, MON3 INT ) G ...

  7. Delphi中TStringList类常用属性方法详解

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...

  8. Java中的equals和hashCode方法详解

    Java中的equals和hashCode方法详解  转自 https://www.cnblogs.com/crazylqy/category/655181.html 参考:http://blog.c ...

  9. 转:Java中的equals和hashCode方法详解

    转自:Java中的equals和hashCode方法详解 Java中的equals方法和hashCode方法是Object中的,所以每个对象都是有这两个方法的,有时候我们需要实现特定需求,可能要重写这 ...

随机推荐

  1. Python基础数据类型之int、bool、str

    数据类型:int  bool  str  list  元祖  dict  集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...

  2. javascript学习总结一

    1. 变量提升hoisting 变量提升的意思是在一个变量作用域里定义的变量的声明会被提升到作用域的顶部,这是变量只会被声明,不会被初始化复制,而是undefined. 代码如下: function ...

  3. ProxySQL 读写分离实践

    前言 ProxySQL是一个高性能的MySQL中间件,拥有强大的规则引擎.具有以下特性: 连接池,而且是 multiplexing 主机和用户的最大连接数限制 自动下线后端DB 延迟超过阀值 ping ...

  4. 使用Navicat Premium 链接本地数据库的方法(二)

    最早一篇:http://www.cnblogs.com/zhengyeye/p/6363179.html 现在又重新装了电脑系统,需遇到了同样的问题.恰巧记得之前自己写的文档,没准可以帮助自己解决掉这 ...

  5. ASP.NET Core + Docker +Jenkins 实现持续集成

    1.新建一个ASP.NET Core MVC应用程序: 2.将其上传到git: 3.建立Jenkins任务 (1)选择"新建任务",输入任务名称,选择"自由风格项目&qu ...

  6. 再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结

    这篇是对angularJS的一些疑点回顾,是对目前angularJS开发的各种常见问题的整理汇总.如果对文中的题目全部了然于胸,觉得对整个angular框架应该掌握的七七八八了.希望志同道合的通知补充 ...

  7. java String的各种方法及操作

    No. 方法名称 功能 字符与字符串 01 public String(char[] value) 将字符数组中所有内容变为字符串 02 public String(char[] value,int ...

  8. webpack构建react项目(一)

    前言 下面是我们使用到技术栈: webpack + react + redux + react-router + react-thunk + ES6 + .... 注意事项: 建议使用npm5.X 或 ...

  9. 机器学习技法:14 Radial Basis Function Network

    Roadmap RBF Network Hypothesis RBF Network Learning k-Means Algorithm k-Means and RBF Network in Act ...

  10. 【转载自netfocus博客】聚合(根)、实体、值对象精炼思考总结

    1.内容摘要 最近在看DDD领域驱动设计,看到实体(Entity),值对象 (Value Object),以及聚合根(Aggregate Root) 时.对他们的关系有些模糊,不清楚.于是去找了找资料 ...