没N,有一个N,有多个N , 3个例子:
SQL> with t
2 as (select 1 id, 10 dt, 'S' ind from dual
3 union all
4 select 1 id, 20 dt, 'S' ind from dual
5 union all
6 select 1 id, 30 dt, 'S' ind from dual
7 union all
8 select 2 id, 10 dt, 'S' ind from dual
9 union all
10 select 2 id, 20 dt, 'S' ind from dual
11 union all
12 select 2 id, 30 dt, 'S' ind from dual
13 union all
14 select 3 id, 30 dt, 'S' ind from dual
15 union all
16 select 3 id, 40 dt, 'S' ind from dual
17 union all
18 select 3 id, 50 dt, 'S' ind from dual)
19select a.id,b.max_dt max_dt
20from (
21select id
22from t
23 where exists (select 'x'
24
from t
25
where ind = 'N')
26 group by t.id) a,
27(select max(lag_dt) max_dt
28
from (
29
select id, dt, ind,lag(dt) over(partition by id order by dt) lag_dt
30
from t
31
where id in (select id
32
from t
33
where ind = 'N')
34
)
35 ) b
36union all
37select id, max(dt) max_dt
38from t
39 where not exists (select 'x'
40
from t
41
where ind = 'N')
42group by id
43order by id
44/
ID MAX_DT
---------- ----------
1 30
2 30
3 50
SQL>
SQL>
SQL>
SQL>
SQL> with t
2 as (select 1 id, 10 dt, 'S' ind from dual
3 union all
4 select 1 id, 20 dt, 'S' ind from dual
5 union all
6 select 1 id, 30 dt, 'N' ind from dual
7 union all
8 select 2 id, 10 dt, 'S' ind from dual
9 union all
10 select 2 id, 20 dt, 'S' ind from dual
11 union all
12 select 2 id, 30 dt, 'S' ind from dual
13 union all
14 select 3 id, 30 dt, 'S' ind from dual
15 union all
16 select 3 id, 40 dt, 'S' ind from dual
17 union all
18 select 3 id, 50 dt, 'S' ind from dual)
19select a.id,b.max_dt max_dt
20from (
21select id
22from t
23 where exists (select 'x'
24
from t
25
where ind = 'N')
26 group by t.id) a,
27(select max(lag_dt) max_dt
28
from (
29
select id, dt, ind,lag(dt) over(partition by id order by dt) lag_dt
30
from t
31
where id in (select id
32
from t
33
where ind = 'N')
34
)
35 ) b
36union all
37select id, max(dt) max_dt
38from t
39 where not exists (select 'x'
40
from t
41
where ind = 'N')
42group by id
43order by id
44/
ID MAX_DT
---------- ----------
1 20
2 20
3 20
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> with t
2 as (select 1 id, 10 dt, 'S' ind from dual
3 union all
4 select 1 id, 20 dt, 'S' ind from dual
5 union all
6 select 1 id, 30 dt, 'N' ind from dual
7 union all
8 select 2 id, 10 dt, 'S' ind from dual
9 union all
10 select 2 id, 20 dt, 'S' ind from dual
11 union all
12 select 2 id, 30 dt, 'S' ind from dual
13 union all
14 select 3 id, 30 dt, 'S' ind from dual
15 union all
16 select 3 id, 40 dt, 'S' ind from dual
17 union all
18 select 3 id, 50 dt, 'N' ind from dual)
19select a.id,b.max_dt max_dt
20from (
21select id
22from t
23 where exists (select 'x'
24
from t
25
where ind = 'N')
26 group by t.id) a,
27(select max(lag_dt) max_dt
28
from (
29
select id, dt, ind,lag(dt) over(partition by id order by dt) lag_dt
30
from t
31
where id in (select id
32
from t
33
where ind = 'N')
34
)
35 ) b
36union all
37select id, max(dt) max_dt
38from t
39 where not exists (select 'x'
40
from t
41
where ind = 'N')
42group by id
43order by id
44/
ID MAX_DT
---------- ----------
1 40
2 40
3 40
SQL>
|