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. 在 IntelliJ IDEA 中配置 Spark(Java API) 运行环境

    1. 新建Maven项目 初始Maven项目完成后,初始的配置(pom.xml)如下: 2. 配置Maven 向项目里新建Spark Core库 <?xml version="1.0& ...

  2. 给VMware下的Linux扩展磁盘空间(以CentOS6.3为例)

    参照这篇文章进行的,但是和作者的步骤有些不一样. #查看挂载点:df -h#显示: 文件系统 容量 已用 可用 已用%% 挂载点/dev/mapper/vg_dc01-lv_root 47G 12G ...

  3. [转帖] k8s kubectl 命令行技巧

    https://jimmysong.io/posts/kubectl-cheatsheet/ Kubectl Cheatsheet kubectl命令技巧大全Posted on November 3, ...

  4. Win2008r2 设置 多用户同时远程

    Study From http://blog.sina.com.cn/s/blog_7ebe66420101tfln.html 1. 启动远程桌面,关闭防火墙 略过不提 2. 添加远程服务角色, 打开 ...

  5. shell 命令 if [ -d filename] 判断文件

    作者:曹毅涵  [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊 ...

  6. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  7. UVa 10305 - Ordering Tasks (拓扑排序裸题)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Kangax 的 ES7 兼容性表格

    Kangax 的 ES7 兼容性表格 https://kangax.github.io/compat-table/es2016plus/ Sort by             Engine type ...

  9. 如何整合Office Web Apps至自己开发的系统(二)

    WOPI项目的创建 首先用vs2012创建一个mvc4的程序.如图: 从上一篇我们可以知道,WOPI通讯主要通过两个服务: 一个是CheckFileInfo服务, 一个是GetFile服务. 所以下面 ...

  10. angular的一次小错误

    前台页面的错误: 在使用angular的时候,发现了标签等不能解析,忙了一个小时没找见错误在哪,最后才发现,原来ng-app,ng-controller等声明错了地方,声明在了div上,而不是在bod ...