select distinct account_num from account order by account_num;
以上查询给出了以下结果
account_num
1
2
4
7
12
18
24
37
45
59
我想将account_num列拆分为三个account_num的元组,如(1,2,4);(7,12,18);(24,37,45),(59);最后一个元组只有一个条目,因为没有更多的account_num。现在我想要一个查询来输出每个元组的最小值和最大值。 (请注意,一个元组的最大值小于下一个元组的最小值)。所需的输出如下所示
1 4
7 18
24 45
59 59
编辑:我已经以最好的方式解释了我的要求
答案 0 :(得分:2)
您可以使用以下示例作为划痕,这仅基于您目前提供的信息。有关进一步的文档,您可以参考Oracle的analytical functions文档:
with src as( --create a source data
select 1 col from dual union
select 2 from dual union
select 4 from dual union
select 7 from dual union
select 12 from dual union
select 18 from dual union
select 24 from dual union
select 37 from dual union
select 45 from dual union
select 59 from dual
)
select
col,
decode(col_2, 0, max_col, col_2) col_2 -- for the last row we get the maximum value for the row
from (
select
col,
lead(col, 2, 0) over (order by col) col_2, -- we get the values from from two rows behind
max(col) over () max_col, -- we get the max value to be used for the last row in the result
rownum rn from src -- we get the rownum to handle the final output
) where mod(rn - 1, 3) = 0 -- only get rows having a step of two
答案 1 :(得分:0)
请添加有关您要执行的操作的详细信息。 account_number 1和4,7和18之间有什么联系?有没有?如果没有,为什么要将其拆分为两列,拆分它的规则是什么? 根据您发布的内容,您可以执行以下操作:
select 1 as account_num, 4 as account_num1 from dual
union all select 7 as account_num, 18 as account_num1 from dual
...
等等,但我没有看到它的用途。
答案 2 :(得分:0)
这是另一种解决方案。
SELECT *
FROM (SELECT DISTINCT MIN(val) over(PARTITION BY gr) min_,
MAX(val) over(PARTITION BY gr) max_
FROM (SELECT val,
decode(trunc(rn / 3), rn / 3, rn / 3, ceil(rn / 3)) gr
FROM (SELECT val,
row_number() over(ORDER BY val) rn
FROM (select distinct account_num from account order by account_num)))) ORDER BY min_
<强>已更新强>
没有分析功能的解决方案。
SELECT MIN(val) min_,
MAX(val) max_
FROM (SELECT val,
ceil(rn / 3) gr
FROM (SELECT val,
rownum rn
FROM A_DEL_ME)) GROUP BY gr