Transposing and aggregating Oracle column data(转置和聚合 Oracle 列数据)
问题描述
我有以下数据
Base End
RMSA Item 1
RMSA Item 2
RMSA Item 3
RMSB Item 1
RMSB Item 2
RMSC Item 4
我想把它转换成下面的格式
I want to convert it to the following format
Key Products
RMSA;RMSB Item 1, Item 2
RMSA Item 3
RMSC Item 4
基本上,那些具有相似结果的应该归为 1 行.但是,由于我在两列上分组,因此我似乎无法使用 listagg 等使其工作.
Basically, those with similar results should be grouped into 1 line. However, I can't seem to get it to work using listagg, etc since I'm grouping on two columns.
有没有办法通过直接的 Oracle 查询来做到这一点?
Is there any way to do this with a direct Oracle query?
推荐答案
你可以使用listagg()窗口解析函数twice作为
You can use listagg() window analytic function twice as
with t1( Base, End ) as
(
select 'RMSA','Item 1' from dual union all
select 'RMSA','Item 2' from dual union all
select 'RMSA','Item 3' from dual union all
select 'RMSB','Item 1' from dual union all
select 'RMSB','Item 2' from dual union all
select 'RMSC','Item 4' from dual
),
t2 as
(
select
listagg(base,';') within group (order by end)
as key,
end
from t1
group by end
)
select key,
listagg(end,',') within group (order by end)
as Products
from t2
group by key
order by products;
Key Products
--------- --------------
RMSA;RMSB Item 1, Item 2
RMSA Item 3
RMSC Item 4
演示
这篇关于转置和聚合 Oracle 列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:转置和聚合 Oracle 列数据
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
