A | B | D | E |
K2 | 1 | 1 | |
K2 | 2 | 1 | |
K2 | 3 | 1 | 20170119 |
K2 | 3 | 2 | 20170119 |
K2 | 4 | 1 | 20170119 |
K2 | 5 | 1 | 20170119 |
K2 | 6 | 1 |
select a,b,rank() over(partition by B order by E ) D , E from test
↑위데이터 쿼리문
A | B | C | D | E | F | G | H |
K2 | 1 | 1 | 1 | 2 | 3 | ||
K2 | 2 | 1 | 1 | 2 | 3 | ||
K2 | 3 | 34 | 1 | 20170119 | 1 | ||
K2 | 3 | 34 | 2 | 20170119 | 2 | ||
K2 | 4 | 34 | 1 | 20170119 | 1 | ||
K2 | 5 | 34 | 1 | 20170119 | 1 | ||
K2 | 6 | 1 | 1 | 2 | 3 |
SELECT A , B , TRUNC(SYSDATE - TO_DATE(E,'YYYYMMDDHH24MISS'),0) AS C , D , E , CASE WHEN D = '1' THEN '1' END F , CASE WHEN D = '2' OR E IS NULL THEN '2' END G , CASE WHEN D = '3' OR E IS NULL THEN '3' END H FROM TEST WHERE D < 4
↑위데이터 쿼리문
안녕하세요. 질문좀 드리겠습니다.(질문잘못해도 양해좀 ㅠ)
지금 나와있는 데이터에서 B컬럼에 중복없이
1. where F = 1
2. where G = 2
3. where H = 3
이렇게 했을경우
A | B | C | D | E | F | G | H |
K2 | 1 | 1 | 1 | 2 | 3 | ||
K2 | 2 | 1 | 1 | 2 | 3 | ||
K2 | 3 | 34 | 1 | 20170119 | 1 | 3 | |
K2 | 3 | 34 | 2 | 20170119 | 2 | ||
K2 | 4 | 34 | 1 | 20170119 | 1 | 2 | 3 |
K2 | 5 | 34 | 1 | 20170119 | 1 | 2 | 3 |
K2 | 6 | 1 | 1 | 2 | 3 |
이렇게 표현하고싶습니다. 근데 H가 3일경우는 D에 3이 없어서 C가 null 로 나와야 하는데 조회를 잘못한건가요.ㅠ
with test (a, b, d, e) as ( select 'K2','1','1',null from dual union all select 'K2','2','1',null from dual union all select 'K2','3','1','20170119' from dual union all select 'K2','3','2','20170119' from dual union all select 'K2','4','1','20170119' from dual union all select 'K2','5','1','20170119' from dual union all select 'K2','6','1',null from dual ) ,t as ( select a , b , trunc(sysdate - to_date(e,'YYYYMMDDHH24MISS'),0) as c , d , e , case when d = '1' then '1' end f , case when d = '2' or e is null then '2' end g , case when d = '3' or e is null then '3' end h , row_number() over(partition by a, b order by b) rn from test where d < 4 ) select a,b,c,d,e ,nvl(f,(case when fmax is null then case when rn = fmin then fval end end)) f ,nvl(g,(case when gmax is null then case when rn = gmin then gval end end)) g ,nvl(h,(case when hmax is null then case when rn = hmin then hval end end)) h from ( select a,b,c,d,e,f,g,h ,max(f) over(partition by a, b) fmax ,max(g) over(partition by a, b) gmax ,max(h) over(partition by a, b) hmax ,min(nvl2(f,null,rn)) over(partition by a, b order by rn) fmin ,min(nvl2(g,null,rn)) over(partition by a, b order by rn) gmin ,min(nvl2(h,null,rn)) over(partition by a, b order by rn) hmin ,rn from t ), (select :f fval, :g gval, :h hval from dual)