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. php四排序-冒泡排序

      算法和数据结构是一个编程工作人员的内功,技术牛不牛,一般都会看这两点.作为php程序员, 提升技能当然也得学习算法. 下面介绍四种入门级排序算法: 冒泡排序.选择排序.插入排序.快速排序.   一 ...

  2. OneZero第五周第一次站立会议(2016.4.18)

    1. 时间: 13:00--13:15  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  3. BZOJ4520 CQOI2016K远点对(KD-Tree+堆)

    堆维护第k大,每个点KD-Tree上A*式查询较远点,跑得飞快,复杂度玄学. #include<iostream> #include<cstdio> #include<c ...

  4. Strongly connected HDU - 4635(判断强连通图 缩点)

    找出强联通块,计算每个连通块内的点数.将点数最少的那个连通块单独拿出来,其余的连通块合并成一个连通分量. 那么假设第一个连通块的 点数是 x  第二个连通块的点数是 y 一个强连通图(每两个点之间,至 ...

  5. 【刷题】LOJ 2818 「eJOI2018」循环排序

    题目描述 本题译自 eJOI2018 Problem F「Cycle Sort」 给定一个长为 \(n\) 的数列 \(\{a_i\}\) ,你可以多次进行如下操作: 选定 \(k\) 个不同的下标 ...

  6. 【BZOJ4870】组合数问题(动态规划,矩阵快速幂)

    [BZOJ4870]组合数问题(动态规划,矩阵快速幂) 题面 BZOJ 洛谷 题解 显然直接算是没法做的.但是要求的东西的和就是从\(nk\)个物品中选出模\(k\)意义下恰好\(r\)个物品的方案数 ...

  7. 学习5_STM32--外设通信方式

    就拿stm32的外设spi来说,通信方式主要有3种 > spi常规收发方式        (在轮询机制下通过判断缓冲区空与非空作为收发依据) > spi中断收发方式 (在中断机制下收发数据 ...

  8. Codeforces Round #541

    因为这次难得不在十点半(或是更晚),大家都在打,然后我又双叒叕垫底了=.= 自己对时间的分配,做题的方法和心态还是太蒻了,写的时候经常写一半推倒重来.还有也许不是自己写不出来,而是在开始写之前就觉得自 ...

  9. C#代码连接Oracle数据库一段时间以后[connection lost contact]的问题

    最近在使用C#代码连接Oracle数据库,分为两部分,WCF的客户端与服务端.程序启动与运行都没有问题,部署到服务器上后,运行也没有问题.但是第二天再访问的时候,就会抛出下边所示的异常.这是怎么回事? ...

  10. npm 设置地址

    -- 查看当前地址: npm config get registry https://registry.npmjs.org/ npm config get disturl undefined -- 设 ...