转置和聚合 Oracle 列数据

Posted

技术标签:

【中文标题】转置和聚合 Oracle 列数据【英文标题】:Transposing and aggregating Oracle column data 【发布时间】:2019-02-18 03:18:03 【问题描述】:

我有以下数据

Base          End
RMSA          Item 1
RMSA          Item 2
RMSA          Item 3
RMSB          Item 1
RMSB          Item 2
RMSC          Item 4

我想把它转换成下面的格式

    Key           Products
    RMSA;RMSB     Item 1, Item 2
    RMSA          Item 3
    RMSC          Item 4

基本上,结果相似的应该归为 1 行。但是,我似乎无法使用 listagg 等使其工作,因为我在两列上进行分组。

有没有办法通过直接 Oracle 查询来做到这一点?

【问题讨论】:

【参考方案1】:

你可以使用listagg()窗口解析函数两次作为

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  

Demo

【讨论】:

【参考方案2】:

下面是一种方式-

WITH base 
     AS (SELECT 'RMSA'   AS base, 
                'Item 1' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSA'   AS base, 
                'Item 2' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSA'   AS base, 
                'Item 3' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSB'   AS base, 
                'Item 1' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSB'   AS base, 
                'Item 2' AS end1 
         FROM   dual 
         UNION 
         SELECT 'RMSC'   AS base, 
                'Item 4' AS end1 
         FROM   dual), 
     t11 
     AS (SELECT t1.base base1, 
                t1.end1 AS end11, 
                t2.base base2, 
                t2.end1 AS end12 
         FROM   base t1 
                inner join base t2 
                        ON t1.end1 = t2.end1 
         WHERE  t1.base > t2.base) SELECT 
Concat(Concat(t11.base1, ';'), t11.base1), 
       Listagg(t11.end11, ',') 
         within GROUP (ORDER BY t11.end11) 
FROM   t11 
GROUP  BY Concat(Concat(t11.base1, ';'), t11.base1) 
--above query will get you results where you have similar results 
UNION 
SELECT t1.base, 
       t1.end1 
FROM   base t1 
       left outer join t11 
                    ON t1.base = t11.base1 
                       AND t1.end1 = t11.end11 
       left outer join t11 t12 
                    ON t1.base = t12.base2 
                       AND t1.end1 = t12.end11 
WHERE  t11.base1 IS NULL 
       AND t12.base2 IS NULL; 

希望对你有帮助

【讨论】:

所提供的代码假定“base”最多有两条记录在一行中匹配。如果数据中有额外的“RMSD/Item 1”配对,返回的结果将不起作用....

以上是关于转置和聚合 Oracle 列数据的主要内容,如果未能解决你的问题,请参考以下文章

Spark:如何使用嵌套数组转置和分解列

SQL 如何将数据转置和分组到静态列中? [复制]

SQL转置和添加列

Oracle如何将多行转置为聚合列

c++矩阵的转置和快速转置

对列名排序后转置和添加前缀的宏