oracle 具有非空值和“分组依据”的SQL有序列

rjee0c15  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(244)

我有这些结果:

COL1  | COL2  | COL3  | COL4  | COL5  | ID  |
-------|-------|-------|-------|-------|-----|
   1   |   0   |   0   |   0   |   0   |  9  |
   0   |   0   |   0   |   0   |   5   |  9  |
   0   |   2   |   0   |   0   |   0   |  9  |
   0   |   0   |   0   |   4   |   0   |  9  |

我想顺序值与非空值第一组ID列;

COL1  | COL2  | COL3  | COL4  | COL5  | ID  |
-------|-------|-------|-------|-------|-----|
   1   |   2   |   4   |   5   |   0   |  9  |

我的查询;
select max(COL1), max(COL2), max(COL3), max(COL4), max(COL5), ID FROM MY_TABLE GROUP BY ID
于是,我问他。

COL1  | COL2  | COL3  | COL4  | COL5  | ID  |
-------|-------|-------|-------|-------|-----|
   1   |   2   |   0   |   4   |   5   |  9  |

EDIT实际上my_table是子查询表。子查询可能会混淆。我原来的table是:

ID | VALUE | SPEC_ID  
----|-------|--------
 1  |   1   |    9   
 2  |   2   |    9   
 3  |   4   |    9   
 4  |   5   |    9   
 5  |   1   |    8   
 6  |   3   |    8

我想要:

COL1  | COL2  | COL3  | COL4  | COL5  | ID  |
-------|-------|-------|-------|-------|-----|
   1   |   2   |   4   |   5   |   0   |  9  |
   1   |   3   |   0   |   0   |   0   |  8  |
vom3gejh

vom3gejh1#

在Oracle中,您可以使用ROW_NUMBER分析函数对行进行排序,然后使用PIVOT将它们从行透视到列:

SELECT COALESCE(col1, 0) AS col1,
       COALESCE(col2, 0) AS col2,
       COALESCE(col3, 0) AS col3,
       COALESCE(col4, 0) AS col4,
       COALESCE(col5, 0) AS col5,
       spec_id AS id
FROM   (
  SELECT value,
         spec_id,
         ROW_NUMBER() OVER (PARTITION BY spec_id ORDER BY value) AS rn
  FROM   table_name
)
PIVOT (
  MAX(value)
  FOR rn IN (1 AS col1, 2 AS col2, 3 AS col3, 4 AS col4, 5 AS col5)
);

其中,对于样本数据:

CREATE TABLE table_name ( ID, VALUE, SPEC_ID) AS
SELECT 1, 1, 9 FROM DUAL UNION ALL   
SELECT 2, 2, 9 FROM DUAL UNION ALL
SELECT 3, 4, 9 FROM DUAL UNION ALL
SELECT 4, 5, 9 FROM DUAL UNION ALL
SELECT 5, 1, 8 FROM DUAL UNION ALL
SELECT 6, 3, 8 FROM DUAL;

输出:
| COL1|COL2|COL3|COL4|COL5|ID|
| --------------|--------------|--------------|--------------|--------------|--------------|
| 1|三|0|0|0|八个|
| 1|二|四|五|0|九|
fiddle

ego6inou

ego6inou2#

你没有提到数据库,所以我假设它是PostgreSQL。
这肯定是非关系逻辑,因此查询最终会不必要地变长。您可以:

select
  max(case when rn = 1 then c end) as col1,
  max(case when rn = 2 then c end) as col2,
  max(case when rn = 3 then c end) as col3,
  max(case when rn = 4 then c end) as col4,
  max(case when rn = 5 then c end) as col5,
  id
from (
  select x.*, row_number() over(partition by id order by rk) as rn
  from (
    select id, max(col1) as c, 1 as rk from t group by id
    union all select id, max(col2), 2 as rk from t group by id
    union all select id, max(col3), 3 as rk from t group by id
    union all select id, max(col4), 4 as rk from t group by id
    union all select id, max(col5), 5 as rk from t group by id
  ) x
  where c <> 0
) y
group by id

结果:

col1  col2  col3  col4  col5  id 
 ----- ----- ----- ----- ----- -- 
 1     3     null  null  null  8  
 1     2     4     5     null  9

请参见db<>fiddle上的运行示例。

EDIT:我更新了按ID分组的解决方案。

相关问题