行转列

这是一个Oracle的列转行函数:LISTAGG()

先看示例代码:

  1. with temp as(
  2. select 'China' nation ,'Guangzhou' city from dual union all
  3. select 'China' nation ,'Shanghai' city from dual union all
  4. select 'China' nation ,'Beijing' city from dual union all
  5. select 'USA' nation ,'New York' city from dual union all
  6. select 'USA' nation ,'Bostom' city from dual union all
  7. select 'Japan' nation ,'Tokyo' city from dual
  8. )
  9. select nation,listagg(city,',') within GROUP (order by city)
  10. from temp
  11. group by nation

这是最基础的用法:

LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)

用法就像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来。

非常方便。

同样是聚合函数,还有一个高级用法:

就是over(partition by XXX)

也就是说,在你不实用Group by语句时候,也可以使用LISTAGG函数:

  1. with temp as(
  2. select 500 population, 'China' nation ,'Guangzhou' city from dual union all
  3. select 1500 population, 'China' nation ,'Shanghai' city from dual union all
  4. select 500 population, 'China' nation ,'Beijing' city from dual union all
  5. select 1000 population, 'USA' nation ,'New York' city from dual union all
  6. select 500 population, 'USA' nation ,'Bostom' city from dual union all
  7. select 500 population, 'Japan' nation ,'Tokyo' city from dual
  8. )
  9. select population,
  10. nation,
  11. city,
  12. listagg(city,',') within GROUP (order by city) over (partition by nation) rank
  13. from temp

总结:LISTAGG()把它当作SUM()函数来使用就可以了。

oracle 行转列 列转行的更多相关文章

  1. oracle 行转列、列转行

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

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

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

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

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

  4. Oracle 行转列及列转行

    参考网址:http://blog.163.com/fushahui_1988@126/blog/static/82879994201192844355174/ 一.多行转一列select id, vn ...

  5. Oracle 多行变一列的方法

    多行变一列的方法有很多,觉得这个第一眼看懂了当时就用的这个办法. 情况是这样的.以下数据前几列是一样的,需要把VAT_VALUE_CHAR 的值放在同一行上. SELECT * FROM ps_vat ...

  6. oracle 多行转多列查询

     oracle 多行转多列查询  ---create table Fruit(id int,name varchar(20), Q1 int, Q2 int, Q3 int, Q4 int);inse ...

  7. oracle 行转列 分析函数

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

  8. Oracle 多行转多列

    Oracle 多行转多列,列值转为列名   前段时间做调查问卷,客户创建自定义问卷内容,包括题目和选项内容; 之后需要到处问卷明细,,,,麻烦来咯 于是到网上到处搜索,没有直接结果;于是又找各种相似的 ...

  9. SQL Server 行转列,列转行。多行转成一列

    一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...

  10. Oracle字符串行拆分成列的三种方式

    Oracle字符串行拆分成列的三种方式 --muphy 开发过程中经常会遇到将前台多个值用逗号连接一同传递到后台查询,这个用逗号连接的字符串分隔的每个字符串分别对应Oracle数据库表的不同行. 如下 ...

随机推荐

  1. javaSE基础之 ArrayList的底层简单实现

    最近就是想扒一扒存在硬盘里面的学习资料(突然想到什么),把以前写过的一些东西整理一下分享出来. 这边是ArrayList 的简单实现,当然只实现了部分方法 package com.yck.collec ...

  2. Hadoop百度百科

    http://baike.baidu.com/link?url=-lfWMjGNGBJxKC1QKKhefXvB7Wou6Ztn8mgeZf8u-1iH5fcf25lbRfqpW1SGwOmQL0JI ...

  3. sizeof(void)有什么用

    偶然发现在C中sizeof(void)是合法的,于是,对它的作用产生了疑问.查阅资料在GNU文档中发现如下解释: In GNU C, addition and subtraction operatio ...

  4. 快速排序/快速查找(第k个, 前k个问题)

    //快速排序:Partition分割函数,三数中值分割 bool g_bInvalidInput = false; int median3(int* data, int start, int end) ...

  5. 工具类:将其他编码类型转换成UTF-8或者其他类型的工具类

    将其他编码类型转换成UTF-8或者其他类型的工具类 public static String changeUTF(String str) { String newStr = null; try { n ...

  6. ssh的相关实验

    author:JevonWei 版权声明:原创作品 跨主机ssh连接 主机A想连接主机C,但是主机C防火墙等原因禁止主机A连接,而主机A可以连接主机B,主机B也可连接主机C,即主机A就可通过主机B做跳 ...

  7. Spark算子讲解(一)

    1:Zip算子 def zip[U](other: RDD[U])(implicit arg0: ClassTag[U]): RDD[(T, U)] 将两个RDD做zip操作,如果当两个RDD分区数目 ...

  8. 一、Nginx安装手册

    1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有g ...

  9. jvm 常用内存分析命令

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt121 // 打印出内存占用情况 jstat -gcutil 12564 10 ...

  10. 九度OJ 1014 排名

    #include <iostream> #include <string.h> #include <sstream> #include <math.h> ...