Oracle:分析函数每次值变化后的小计
Oracle: Analytical functions Sub totals after each change in value
我有以下数据(示例中的记录顺序):
A B
1 10
1 20
1 30
1 40
2 50
2 65
2 75
1 89
1 100
来自SQL:
with x as (
select A, B
from (
select 1 as A, 10 as B from dual
union all
select 1 as A, 20 as B from dual
union all
select 1 as A, 30 as B from dual
union all
select 1 as A, 40 as B from dual
union all
select 2 as A, 50 as B from dual
union all
select 2 as A, 65 as B from dual
union all
select 2 as A, 75 as B from dual
union all
select 1 as A, 89 as B from dual
union all
select 1 as A, 100 as B from dual
)
)
select A, B
from X
我想对 A 列中每次值更改的数据进行分组,
我想得到以下结果:
A MIN(B) MAX(B)
1 10 40
2 50 75
1 89 100
如何在 ORACLE 11 中获得这样的结果。我希望有一个简单的实现...
这是一个 gaps and islands 问题,已使用 row_number
解析函数解决
SELECT a,
MIN(b),
MAX(b)
FROM (
SELECT x.*,
ROW_NUMBER() OVER(
ORDER BY b
) - ROW_NUMBER() OVER(
PARTITION BY a
ORDER BY b
) AS seq
FROM x
)
GROUP BY a,
seq;
我有以下数据(示例中的记录顺序):
A B
1 10
1 20
1 30
1 40
2 50
2 65
2 75
1 89
1 100
来自SQL:
with x as (
select A, B
from (
select 1 as A, 10 as B from dual
union all
select 1 as A, 20 as B from dual
union all
select 1 as A, 30 as B from dual
union all
select 1 as A, 40 as B from dual
union all
select 2 as A, 50 as B from dual
union all
select 2 as A, 65 as B from dual
union all
select 2 as A, 75 as B from dual
union all
select 1 as A, 89 as B from dual
union all
select 1 as A, 100 as B from dual
)
)
select A, B
from X
我想对 A 列中每次值更改的数据进行分组, 我想得到以下结果:
A MIN(B) MAX(B)
1 10 40
2 50 75
1 89 100
如何在 ORACLE 11 中获得这样的结果。我希望有一个简单的实现...
这是一个 gaps and islands 问题,已使用 row_number
解析函数解决
SELECT a,
MIN(b),
MAX(b)
FROM (
SELECT x.*,
ROW_NUMBER() OVER(
ORDER BY b
) - ROW_NUMBER() OVER(
PARTITION BY a
ORDER BY b
) AS seq
FROM x
)
GROUP BY a,
seq;