Oracle列转行函数 Listagg()详解

article/2025/10/4 10:47:51

详解

listagg()函数可以实现多列记录聚合为一条记录,从而实现数据的压缩、致密化(data densification)

基本用法

像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)

示例:
with temp as(  
select '中国' nation ,'江苏' city from dual union all  
select '中国' nation ,'上海' city from dual union all  
select '中国' nation ,'北京' city from dual union all  
select '美国' nation ,'纽约' city from dual union all  
select '美国' nation ,'波士顿' city from dual union all  
select '日本' nation ,'东京' city from dual   
)  
select nation,listagg(city,',') within GROUP (order by city)  as Cities
from temp  
group by nation
查询结果

在这里插入图片描述

高级用法

listagg(XXX,’,’) within GROUP (order by XXX) over (partition by XXX) rank

示例
with temp as(  
select 500 population, '中国' nation ,'江苏' city from dual union all  
select 1500 population, '中国' nation ,'上海' city from dual union all  
select 500 population, '中国' nation ,'北京' city from dual union all  
select 1000 population, '美国' nation ,'纽约' city from dual union all  
select 500 population, '美国' nation ,'波士顿' city from dual union all  
select 500 population, '日本' nation ,'东京' city from dual   
)  
select population,  
nation,  
city,  
listagg(city,',') within GROUP (order by city) over (partition by nation) rank  
from temp
运行结果

在这里插入图片描述

Oracle Database SQL Language Reference上有关listagg()函数的描述如下:


Purpose
For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column.

  1. As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.
  2. As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.
  3. As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause.
    The arguments to the function are subject to the following rules:
  4. The measure_expr can be any expression. Null values in the measure column are ignored.
  5. The delimiter_expr designates the string that is to separate the measure values.
    This clause is optional and defaults to NULL.
  6. The order_by_clause determines the order in which the concatenated values are returned. The function is deterministic only if the ORDER BY column list achieved
    unique ordering.
    The return data type is RAW if the measure column is RAW; otherwise the return value is VARCHAR2.
    Aggregate Examples
    The following single-set aggregate example lists all of the employees in Department 30 in the hr.employees table, ordered by hire date and last name:
    SELECT LISTAGG(last_name, '; ')
    WITHIN GROUP (ORDER BY hire_date, last_name) “Emp_list”,
    MIN(hire_date) “Earliest”
    FROM employees
    WHERE department_id = 30;
    Emp_list Earliest

Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares 07-DEC-02
The following group-set aggregate example lists, for each department ID in the hr.employees table, the employees in that department in order of their hire date:

SELECT department_id “Dept.”,
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date) “Employees”
FROM employees
GROUP BY department_id
ORDER BY department_id;
Dept. Employees


10 Whalen
20 Hartstein; Fay
30 Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares
40 Mavris
50 Kaufling; Ladwig; Rajs; Sarchand; Bell; Mallin; Weiss; Davie
s; Marlow; Bull; Everett; Fripp; Chung; Nayer; Dilly; Bissot
; Vollman; Stiles; Atkinson; Taylor; Seo; Fleaur; Matos; Pat
el; Walsh; Feeney; Dellinger; McCain; Vargas; Gates; Rogers;
Mikkilineni; Landry; Cabrio; Jones; Olson; OConnell; Sulliv
an; Mourgos; Gee; Perkins; Grant; Geoni; Philtanker; Markle
60 Austin; Hunold; Pataballa; Lorentz; Ernst
70 Baer
. . .
Analytic Example
The following analytic example shows, for each employee hired earlier than September 1, 2003, the employee’s department, hire date, and all other employees in
that department also hired before September 1, 2003:
SELECT department_id “Dept”, hire_date “Date”, last_name “Name”,
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date, last_name)
OVER (PARTITION BY department_id) as “Emp_list”
FROM employees
WHERE hire_date < ‘01-SEP-2003’
ORDER BY “Dept”, “Date”, “Name”;
Dept Date Name Emp_list


30 07-DEC-02 Raphaely Raphaely; Khoo
30 18-MAY-03 Khoo Raphaely; Khoo
40 07-JUN-02 Mavris Mavris
50 01-MAY-03 Kaufling Kaufling; Ladwig
50 14-JUL-03 Ladwig Kaufling; Ladwig
70 07-JUN-02 Baer Baer
90 13-JAN-01 De Haan De Haan; King
90 17-JUN-03 King De Haan; King
100 16-AUG-02 Faviet Faviet; Greenberg
100 17-AUG-02 Greenberg Faviet; Greenberg
110 07-JUN-02 Gietz Gietz; Higgins
6-AUG-02 Faviet Faviet; Greenberg
100 17-AUG-02 Greenberg Faviet; Greenberg
110 07-JUN-02 Gietz Gietz; Higgins
110 07-JUN-02 Higgins Gietz; Higgins


http://chatgpt.dhexx.cn/article/00bK1R1d.shtml

相关文章

Oracle 中 LISTAGG 函数的介绍以及使用

LISTAGG 函数介绍 listagg 函数是 Oracle 11.2 推出的新特性。 其主要功能类似于 wmsys.wm_concat 函数&#xff0c; 即将数据分组后&#xff0c; 把指定列的数据再通过指定符号合并。LISTAGG 使用 listagg 函数有两个参数&#xff1a;1、 要合并的列名2、 自定义连接符号☆L…

矩阵乘以其矩阵转置求导-数学

20210703 矩阵论 https://zhuanlan.zhihu.com/p/288541909?utm_sourcewechat_session 矩阵运算法则 20210529 https://blog.csdn.net/Lisa_Ren_123/article/details/81983785 矩阵转置求导 https://jingyan.baidu.com/article/da1091fb69f0b7027849d612.html

matlab中怎么求矩阵的转置

第一步我们首先需要知道matlab中矩阵后面加单引号是共轭转置&#xff0c;加点和单引号是转置&#xff0c;如下图所示&#xff1a; matlab中怎么求矩阵的转置 第二步在matlab命令行窗口中输入“ A[1 2 4;5 6 7]”&#xff0c;如下图所示&#xff1a; matlab中怎么求矩阵的转…

矩阵的转置等于矩阵的逆

http://zhidao.baidu.com/question/334500638.html 百度知道三个回答 矩阵A的转置矩阵A^T等于A的逆矩阵A^-1 那么AA^TAA^-1E 设A(α1&#xff0c;α2&#xff0c;α3&#xff0c;...&#xff0c;αn)^T&#xff0c;其中αi为n维列向量&#xff0c; 那么A^T(α1&#xff0c;α2&…

矩阵转置运算简单总结

矩阵转置的运算规律&#xff1a; &#xff08;1&#xff09;; &#xff08;2&#xff09;; &#xff08;3&#xff09;; &#xff08;4&#xff09;; &#xff08;5&#xff09;

矩阵的转置

稀疏矩阵&#xff1a; 当一个矩阵中的很多元素都是零&#xff0c;而且非零元素的分布没有规律时&#xff0c;该矩阵称为稀疏矩阵。 稀疏矩阵的压缩存储方法&#xff1a; 从稀疏矩阵的概念&#xff0c;我们可以知道&#xff0c;稀疏矩阵的大多元素都是零。所以&#xff0c;我们只…

转置矩阵,逆矩阵和倒转置矩阵

单位矩阵&#xff1a; 转置矩阵(transpose matrix) 在线性代数中&#xff0c;矩阵A的转置是另一个矩阵AT&#xff08;也写做Atr, tA或A′&#xff09;由下列等价动作建立: 把A的横行写为AT的纵列把A的纵列写为AT的横行 形式上说&#xff0c;m n矩阵A的转置是n m矩阵 for 。…

矩阵的乘法和转置

矩阵的乘法 矩阵的转置 A的转置为 ​​​​​​​ 反对称矩阵主对角线上全为0&#xff1b;

矩阵与转置

1.转置矩阵 1.1转置矩阵简介 把矩阵A的行换成同序数的列得到的新矩阵&#xff0c;叫做A的转置矩阵(Transpose of a Matrix)&#xff0c;记作ATAT。 例如&#xff1a; 因此&#xff0c;转置矩阵的特点&#xff1a; &#xff08;1&#xff09;转置矩阵的行数是原矩阵的列数&…

矩阵转置

矩阵转置 1. n*n 对角线置换 #include <stdio.h> //编写函数&#xff1a;实现4*4矩阵的转置。 //要求&#xff1a;在main函数中定义二维数组、输入数据、输出原矩阵、调用函数、输出转置后的矩阵。 int ZhuanZhi(int arr[][4]){int temp;for(int i0;i<4;i){ for(int …

矩阵乘以它的转置

矩阵乘以它的转置 AA^T| |A| |A^T| |A||A| |A|^2即矩阵A乘以A的转置等于A的行列式的平方。 明显不等于啦&#xff0c;1*2的矩阵转置矩阵为2*1&#xff0c;那么1*2的矩阵乘以2*1的转置矩阵得到一个1*1的矩阵&#xff0c;而2*1的转置矩阵乘以1*2的矩阵得到一个2*2的矩阵 这个…

矩阵转置与矩阵相乘

1.转置矩阵 1.1转置矩阵简介 把矩阵A的行换成同序数的列得到的新矩阵&#xff0c;叫做A的转置矩阵(Transpose of a Matrix)&#xff0c;记作ATAT。 例如&#xff1a; 因此&#xff0c;转置矩阵的特点&#xff1a; &#xff08;1&#xff09;转置矩阵的行数是原矩阵的列数&…

gyp linux,使用gyp

GYP(Generate You Project)&#xff0c;生成IDE项目的工具&#xff0c;使用Python脚本写成&#xff0c;配置文件为JSON格式。 使用gyp需要两个环境&#xff0c;python和gyp。gyp可以直接在这里下载 git clone https://chromium.googlesource.com/external/gyp 一般下载到build/…

gyp ERR!报错解决

项目安装依赖包的时候&#xff0c;多次出现gyp ERR&#xff01;或者node-pre-gyp ERR!这种错&#xff0c;记录一下最近遇到的报错&#xff1a; 用过两次这种解决办法&#xff0c;都能把依赖下好&#xff0c;项目跑起来 npm instal --unsafe-perm 或者 yarn --unsafe-perm 运…

解决gyp err 错误

npm install 安装失败 有一堆报错&#xff0c;大概意思就是node-gyp没有安装成功&#xff0c;以及需要一些python的环境 解决步骤 1、安装python环境 2、以管理员身份执行 npm install --g --production windows-build-tools 3、之后在安装完成后会在C:WindowsSystem32里找到一…

gyp ERR! find Python 解决方案

命令行报错 npm install npm WARN deprecated fsevents2.1.3: "Please update to latest v2.3 or v2.2" npm WARN deprecated fsevents1.2.13: fsevents 1 will break on node v14 and could be using insecure binaries. Upgrade to fsevents 2.> heapdump0.3.…

node-gyp 报错

error C:\xxx\node_modules\fibers: Command failed. 设置后还是会报错需要下载Visual Studio 访问地址GitHub - nodejs/node-gyp: Node.js native addon build tool 安装完之后设置使用路径就好了 npm config set msvs_version "C:\Program Files (x86)\Microsoft Visual…

【解决】gyp ERR! node -v v12.7.0 gyp ERR! node-gyp -v v3.8.0 gyp ERR! not ok

使用npm install报错如下 原因 这是 node-sass、sass-loader 安装的版本和电脑安装的 node.js 版本不兼容导致的错误 解决办法 我的node.js版本是&#xff1a;v12.7.0 在项目目的package.json文件把 node-sass 和 sass-loader 的修改成如下版本&#xff0c;npm i…