如何使用 oracle 查询找出下面的输出
How to find out below output using oracle query
输入:
col1 col2
------------
1 A
2 1
3 B
4 2
5 C
6 Null
我想要的输出
Col1 Col2
__________
A 1
B 2
C Null
哦,我明白了。假设 col1
中没有间隙,您可以使用算术聚合:
select max(case when mod(id, 2) = 0 then col2 end),
max(case when mod(id, 2) = 1 then col2 end)
from t
group by floor((id - 1) / 2);
另一种方法使用lead()
:
select col2, next_col2
from (select t.*, lead(col2) over (order by id) as next_col2
from t
) t
where mod(id, 2) = 1;
使用连接到相同 table 的 下一个 行并限制到每隔一行。
select t1.col2 col1, t2.col2
from tab t1
join tab t2 on t1.col1 = t2.col1 -1
where mod(t1.col1,2) = 1
C C
- -
A 1
B 2
C
同样,它假定您在 col1
中的序列没有间隙。
这是 PIVOT
的应用程序,自 Oracle 11.1 起可用。 (在 Oracle 12.1 及更高版本中,使用 MATCH_RECOGNIZE
可以使用更短的解决方案,但问题已明确标记为 oracle11g
。)
我使用分析函数 ROW_NUMBER()
来准备数据,因此我们不需要对排序列做任何假设 - 它可能有间隙,and/or 它甚至没有必须是 number
列,它可以是日期或字符串或任何其他可以订购的内容。
设置:
create table sample_data (col1, col2) as
select 1, 'A' from dual union all
select 2, '1' from dual union all
select 3, 'B' from dual union all
select 4, '2' from dual union all
select 5, 'C' from dual union all
select 6, null from dual
;
查询和输出:
select col1, col2
from (
select ceil(row_number() over (order by col1) / 2) as r,
mod (row_number() over (order by col1) , 2) as c, col2
from sample_data
)
pivot (min(col2) for c in (1 as col1, 0 as col2))
order by r
;
COL1 COL2
---- ----
A 1
B 2
C
只是为了好玩,对于在 Oracle 12 或更高版本中可能遇到此问题的人,这里是 match_recognize
解决方案:
select col1, col2
from sample_data
match_recognize(
order by col1
measures x.col2 as col1, y.col2 as col2
pattern ( x y? )
define x as null is null
);
输入:
col1 col2
------------
1 A
2 1
3 B
4 2
5 C
6 Null
我想要的输出
Col1 Col2
__________
A 1
B 2
C Null
哦,我明白了。假设 col1
中没有间隙,您可以使用算术聚合:
select max(case when mod(id, 2) = 0 then col2 end),
max(case when mod(id, 2) = 1 then col2 end)
from t
group by floor((id - 1) / 2);
另一种方法使用lead()
:
select col2, next_col2
from (select t.*, lead(col2) over (order by id) as next_col2
from t
) t
where mod(id, 2) = 1;
使用连接到相同 table 的 下一个 行并限制到每隔一行。
select t1.col2 col1, t2.col2
from tab t1
join tab t2 on t1.col1 = t2.col1 -1
where mod(t1.col1,2) = 1
C C
- -
A 1
B 2
C
同样,它假定您在 col1
中的序列没有间隙。
这是 PIVOT
的应用程序,自 Oracle 11.1 起可用。 (在 Oracle 12.1 及更高版本中,使用 MATCH_RECOGNIZE
可以使用更短的解决方案,但问题已明确标记为 oracle11g
。)
我使用分析函数 ROW_NUMBER()
来准备数据,因此我们不需要对排序列做任何假设 - 它可能有间隙,and/or 它甚至没有必须是 number
列,它可以是日期或字符串或任何其他可以订购的内容。
设置:
create table sample_data (col1, col2) as
select 1, 'A' from dual union all
select 2, '1' from dual union all
select 3, 'B' from dual union all
select 4, '2' from dual union all
select 5, 'C' from dual union all
select 6, null from dual
;
查询和输出:
select col1, col2
from (
select ceil(row_number() over (order by col1) / 2) as r,
mod (row_number() over (order by col1) , 2) as c, col2
from sample_data
)
pivot (min(col2) for c in (1 as col1, 0 as col2))
order by r
;
COL1 COL2
---- ----
A 1
B 2
C
只是为了好玩,对于在 Oracle 12 或更高版本中可能遇到此问题的人,这里是 match_recognize
解决方案:
select col1, col2
from sample_data
match_recognize(
order by col1
measures x.col2 as col1, y.col2 as col2
pattern ( x y? )
define x as null is null
);