wm_concat()函数

--创建表

create table test(id number,name varchar2(20));

--插入数据

insert into test values(1,'a');

insert into test values(1,'b');

insert into test values(1,'c');

insert into test values(2,'d');

insert into test values(2,'e');

--分组合并

select id,wm_concat(name) name from test group by id;

注:有些数据库版本需要转换成字符串类型,否则报错,如:

select id,to_char(wm_concat(name)) name from test group by id;

pivot()函数

需要Oracle版本大于等于11g

--创建表

create table demo(id int,name varchar(20),nums int);

--插入数据

insert into demo values(1, '苹果', 1000);

insert into demo values(2, '苹果', 2000);

insert into demo values(3, '苹果', 4000);

insert into demo values(4, '橘子', 5000);

insert into demo values(5, '橘子', 3000);

insert into demo values(6, '葡萄', 3500);

insert into demo values(7, '芒果', 4200);

insert into demo values(8, '芒果', 5500);

select name, sum(nums) nums from demo group by name;

NAME    NUMS

1    葡萄    3500

2    芒果    9700

3    橘子    8000

4    苹果    7000

select *

from (select sum(nums) 苹果 from demo where name = '苹果'),

(select sum(nums) 橘子 from demo where name = '橘子'),

(select sum(nums) 葡萄 from demo where name = '葡萄'),

(select sum(nums) 芒果 from demo where name = '芒果');

苹果    橘子    葡萄    芒果

1    7000    8000    3500    9700

select * from (select name, nums from demo) pivot(sum(nums) for name in('苹果' 苹果,'橘子','葡萄','芒果'));

苹果    '橘子'    '葡萄'    '芒果'

1    7000    8000    3500    9700

注意:

1、pivot(聚合函数 for 列名 in(类型)) ,其中 in('') 中可以指定别名

2、in中还可以指定子查询,比如 select distinct code from customers

3、接上,按照Oracle文档,如果pivot语句中in后面的列不固定,只能使用xml格式的返回结果

问题:

有时候wm_concat()函数转换后,会把数据变为clob格式,此时需要用to_char()函数进行转换,但可能会报“ORA-22922: 不存在的 LOB 值”异常,解决办法:

1、交换to_char()和wm_concat()函数顺序

to_char(wm_concat(t.site_id)) total_site_id_with_user改为,wm_concat(to_char(t.site_id)) total_site_id_with_user

2、使用listagg()函数替代wm_concat()函数

to_char(listagg(check_day || '(' || model01.count_out_site_id || ')',',') within group (order by model01.count_out_site_id)) count_out_site_id

oracle行转列的更多相关文章

  1. oracle 行转列 分析函数

    oracle 行转列 首先看一下源数据: 方法一:WM_CONCAT group by 这个方法没有问题. SELECT CODE_TS, WMSYS.WM_CONCAT(S_NUM + || ':' ...

  2. Oracle 行转列pivot 、列转行unpivot 的Sql语句总结

    这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str from ap ...

  3. Oracle行转列、列转行的Sql语句总结

    多行转字符串 这个比较简单,用||或concat函数可以实现  SQL Code  12    select concat(id,username) str from app_userselect i ...

  4. Oracle行转列、列转行的Sql语句总结(转)

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_userselect id||username str f ...

  5. oracle 行转列、列转行

    最近做数据处理,经常遇到需要行转列.列转行的场景,记录个非常简单实用的oracle  列转行.行转的列方法 1.行转列,基础数据如下 做行转列处理 处理SQL select user_name,max ...

  6. Oracle行转列操作

    有时候我们在展示表中数据的时候,需要将行转为列来显示,如以下形式: 原表结构展示如下:---------------------------产品名称    销售额     季度------------ ...

  7. oracle行转列(连接字符串函数)

    方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面的参数是列,而且可以是多个列的集 ...

  8. Oracle行转列的函数

    --行转列的函数-- CREATE OR REPLACE FUNCTION Calvin( col IN VARCHAR2,dw IN VARCHAR2) RETURN VARCHAR2 IS ret ...

  9. oracle 行转列 列转行

    行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...

  10. oracle行转列、列转行、连续日期数字实现方式及mybatis下实现方式

    转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9977591.html 九月份复习,十月份考试,十月底一直没法收心,赶在十一初 由于不可抗拒的原因又不得不重新找 ...

随机推荐

  1. 软件工程学习之小学四则混合运算出题软件 Version 1.1 设计思路及感想

    继上次采用形式文法来生成混合运算的算式,由于算法中没有引入控制参数而导致容易产生形式累赘(多余的括号等)的算式.本次更新决定采用一种更为简单有效的生成方式,由给出的一个随机的最终答案S,通过给定的一个 ...

  2. ELK 性能(3) — 在 Docker 上运行高性能容错的 Elasticsearch 集群

    ELK 性能(3) - 在 Docker 上运行高性能容错的 Elasticsearch 集群 介绍 在 Docker 上运行高性能容错的 Elasticsearch 集群 内容 通常熟悉的开发流程是 ...

  3. Beta 冲刺 二

    团队成员 051601135 岳冠宇 031602629 刘意晗 031602248 郑智文 031602330 苏芳锃 031602234 王淇 照片 项目进展 岳冠宇 昨天的困难 fragment ...

  4. delphi XE的字符串处理

    最近用delphi xe做了个东西,因为以前一直使用Delphi 7做开发,delphi 7 到delphi XE有了很大的变化,最大的变化就是对Unicode的支持,所以刚开始使用DELPHI XE ...

  5. wordpress 点击文章图片 不能编辑(chrome下面) wordpress Uncaught DOMException: Failed to execute 'setBaseAndExtent' on 'Selection': There is no child at offset 1.

    说明:在chrome下面,编辑文章插入的图片,点击到图片上面,没有菜单显示. 报错: tinymce.min.js:10 Uncaught DOMException: Failed to execut ...

  6. 在vue中如何动态修改title标签的值

    建议用vue-wechat-title插件为微信动态设置标题 1,首先安装插件 cnpm install vue-wechat-title --save 2,在main.js中引入 Vue.use(r ...

  7. LOJ 530 最小倍数(数论)

    题意 有\(T\)组数据. 给定\(p\),求最小的正整数\(n\),使得\(n!\%p=0\). 由于\(p\)很大,输入将给出\(m\)和\(e_1,e_2...e_m\),表示\(p=\prod ...

  8. [代码]--WinForm 窗体之间相互嵌套

    public FrmScan() { InitializeComponent(); Form1 frm = new Form1(); frm.Dock = DockStyle.Fill; frm.Fo ...

  9. poj 1511 Invitation Cards(最短路中等题)

    In the age of television, not many people attend theater performances. Antique Comedians of Malidine ...

  10. 初识Java Enum

    enum 的全称为 enumeration, 是 JDK 1.5  中引入的新特性,存放在 java.lang 包中. enum是关键字,感觉它跟class.interface是并列的,并且不能跟fi ...