쿼리 질문있습니다.. 0 4 661

by 아빠파파 [Oracle 기초] [2017.02.22 11:25:25]


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 로 나와야 하는데 조회를 잘못한건가요.ㅠ

 

by jkson [2017.02.22 11:56:19]

설명이 많이 누락된 것 같네요. 파라메터로 f값 1, g값 2, h값 3을 넣어주면 a, b로 묶어보았을 때 해당 값이 없으면 값을 넣어주겠다는 건가요? 헷갈리네요.


by 아빠파파 [2017.02.22 12:30:14]

네 파라메터 값을 F = 1, G = 2, H = 3 을 넣었을경우입니다. 데이터는 고정으로 B 1,2,3,4,5,6 나와야 하고요..

F,G,H 를 CASE문으로 해봤는데 잘 안되서 질문드린겁니다 ㅠ


by jkson [2017.02.22 12:54:10]
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)          

 


by 아빠파파 [2017.02.22 15:17:00]

by jkson 감사합니다.^^

여차여차 수정좀 해서 완성했습니다.

좋은하루되세요~

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입