SQL-запрос для преобразования нет. рядов до нет. колонны в оракуле - PullRequest
0 голосов
/ 27 марта 2019

Мне нужна помощь, чтобы преобразовать все строки в столбец.

т.е. столбцы no.of = общее количествозначений в col2 и col3, относящихся к col1.

Сценарий

enter image description here

Попытка запроса:

with cte as (
    select 'A' as col1, 1 as col2, 2 as col3 from dual
    union
    select 'A' as col1, 3 as col2, 4 as col3 from dual
    union
    select 'A' as col1, 5 as col2, 6 as col3 from dual
    union
    select 'B' as col1, 10 as col2, 101 as col3 from dual
    union
    select 'B' as col1, 20 as col2, 202 as col3 from dual
    union
    select 'C' as col1, 50 as col2, 501 as col3 from dual
    union
    select 'C' as col1, 60 as col2, 601 as col3 from dual
    union
    select 'C' as col1, 70 as col2, 701 as col3 from dual
) select * from cte

Как мне написать сводный запрос здесь?

Ответы [ 3 ]

1 голос
/ 27 марта 2019

Один из подходов к этому - использование сводного запроса вместе с ROW_NUMBER:

WITH cte AS (
    SELECT Col1, Col2, Col3,
        ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2) rn
    FROM yourTable
)

SELECT
    Col1,
    MAX(CASE WHEN rn = 1 THEN Col2 END) AS Col2,
    MAX(CASE WHEN rn = 1 THEN Col3 END) AS Col3,
    MAX(CASE WHEN rn = 2 THEN Col2 END) AS Col4,
    MAX(CASE WHEN rn = 2 THEN Col3 END) AS Col5,
    MAX(CASE WHEN rn = 3 THEN Col2 END) AS Col6,
    MAX(CASE WHEN rn = 3 THEN Col3 END) AS Col7
FROM cte
GROUP BY
    Col1;

enter image description here

Demo

1 голос
/ 27 марта 2019
-- Oracle 11g+: pivot
with cte as  (
select 'A' as col1, 1  as col2, 2   as col3 from dual union all
select 'A' as col1, 3  as col2, 4   as col3 from dual union all
select 'A' as col1, 5  as col2, 6   as col3 from dual union all
select 'B' as col1, 10 as col2, 101 as col3 from dual union all
select 'B' as col1, 20 as col2, 202 as col3 from dual union all
select 'C' as col1, 50 as col2, 501 as col3 from dual union all
select 'C' as col1, 60 as col2, 601 as col3 from dual union all
select 'C' as col1, 70 as col2, 701 as col3 from dual )
select *
from
   (select t.*, row_number() over (partition by col1 order by col2) rn
    from cte t
   )
pivot (max(col2) as pc2, max(col3) as pc3 for rn in (1,2,3));

C      1_PC2      1_PC3      2_PC2      2_PC3      3_PC2      3_PC3
- ---------- ---------- ---------- ---------- ---------- ----------
A          1          2          3          4          5          6
B         10        101         20        202
C         50        501         60        601         70        701
0 голосов
/ 27 марта 2019

использование row_number() и условное агрегирование

select col1,
       max(case when rn=1 then col2 end) as col2,
       max(case when rn=2 then col2 end) as col3,
       max(case when rn=3 then col2 end) as col4,
       max(case when rn=1 then col3 end) as col5,
       max(case when rn=2 then col3 end) as col6,
       max(case when rn=3 then col3 end) as col7
from
(
select *, row_number() over(partition by col1 order by null) as rn
from tablename
)A group by col1
...