간단한 쿼리 질문 좀 드릴께요. 0 6 1,605

by 아카시아향기 [2015.07.17 15:18:49]


안녕하세요.

이번에 쿼리를 하나 짜려고 하는데

A테이블   B테이블
key name   key name
1 C   1 C
2 A   2 A
3 B   3 B
4 A   4 A
      5 A
      6 B
      7 C
      8 D
     

9

E

 위와 같이 두개의 테이블이 있는데 B테이블에 VALUE 값 중 한건만 승인이 가능합니다.

다만 여기서 예외 조항이 있는게 A테이블에 있는건 무조건 승인을 해야 하는데요.

결과는 다음과 같이 나와야 합니다.

결과값
1 C 승인
2 A 승인
3 B 승인
4 A 승인
8 D 승인
9 E 승인
5 A 취소
6 B 취소
7 C 취소

한주 마무리 하는 금요일에 머리가 아프네요. ㅜ

답변 달아주시는분께 미리 감사드립니다.

by 초보초보 [2015.07.17 16:18:16]

with a(key,name) as(
  select 1,'C' from dual
  union all
  select 2,'A' from dual
  union all
  select 3,'B' from dual
  union all
  select 4,'A' from dual)
  ,b(key,name) as(
  select 1,'C' from dual
  union all
  select 2,'A' from dual
  union all
  select 3,'B' from dual
  union all
  select 4,'A' from dual
   union all
  select 5,'A' from dual
  union all
  select 6,'B' from dual
  union all
  select 7,'C' from dual
  union all
  select 8,'D' from dual
  union all
  select 9,'E' from dual)
  select key,name,case when cnt2=1 or cnt2 is null or cnt2<=cnt then '승인' else '취소' end as status
  from
  (
  select case when cnt>0 then count(name)over(PARTITION by name order by key) end cnt2,cnt,name,key
  from (select key,name,
  (select count(*) from a where a.name=b.name) as cnt
  from b)
  )
  order by key;


by jkson [2015.07.17 16:21:50]
WITH A AS
(
SELECT 1 KEY, 'C' NAME FROM DUAL
UNION ALL
SELECT 2 KEY, 'A' NAME FROM DUAL
UNION ALL
SELECT 3 KEY, 'B' NAME FROM DUAL
UNION ALL
SELECT 4 KEY, 'A' NAME FROM DUAL
)
,
B AS
(
SELECT 1 KEY, 'C' NAME FROM DUAL
UNION ALL
SELECT 2 KEY, 'A' NAME FROM DUAL
UNION ALL
SELECT 3 KEY, 'B' NAME FROM DUAL
UNION ALL
SELECT 4 KEY, 'A' NAME FROM DUAL
UNION ALL
SELECT 5 KEY, 'A' NAME FROM DUAL
UNION ALL
SELECT 6 KEY, 'B' NAME FROM DUAL
UNION ALL
SELECT 7 KEY, 'C' NAME FROM DUAL
UNION ALL
SELECT 8 KEY, 'D' NAME FROM DUAL
UNION ALL
SELECT 9 KEY, 'E' NAME FROM DUAL
)
SELECT KEY, NAME, CASE WHEN AEX = 'Y' OR RN = 1 THEN '승인' ELSE '취소' END APPYN
  FROM
    ( 
    SELECT B.KEY, B.NAME
          ,ROW_NUMBER() OVER (PARTITION BY B.NAME ORDER BY B.KEY) RN
          ,DECODE(A.KEY,NULL,'N','Y') AEX
      FROM B , A
     WHERE B.KEY = A.KEY(+)
    ORDER BY B.KEY
    )
 ORDER BY APPYN, KEY

우앙 다들 빠르시당ㅋ


by 부쉬맨 [2015.07.17 16:24:02]
with t as
( 
select 1 key , 'C' name from dual union all
select 2 key , 'A' name from dual union all
select 3 key , 'B' name from dual union all
select 4 key , 'A' name from DUAL
) ,
 TT AS(
select 1 key , 'C' name from dual union all
select 2 key , 'A' name from dual union all
select 3 key , 'B' name from dual union all
select 4 key , 'A' name from dual union all
select 5 key , 'A' name from dual union all
select 6 key , 'B' name from dual union all
select 7 key , 'C' name from dual union all
select 8 key , 'D' name from dual union all
select 9 key , 'E' name from dual 
) 
SELECT TT.KEY
        , TT.NAME
        , DENSE_RANK() OVER(ORDER BY T.NAME ASC) A 
        , CASE WHEN T.NAME = TT.NAME THEN '승인' 
               WHEN RANK( ) OVER (PARTITION BY TT.NAME ORDER BY TT.KEY ASC) =1 THEN '승인' ELSE '취소' END 결과   
FROM TT, T
WHERE TT.KEY = T.KEY(+)

 


by 창조의날개 [2015.07.17 16:26:58]

WITH TA(KEY,NAME) AS(
    SELECT 1, 'C' FROM DUAL UNION ALL
    SELECT 2, 'A' FROM DUAL UNION ALL
    SELECT 3, 'B' FROM DUAL UNION ALL
    SELECT 4, 'A' FROM DUAL
)
, TB(KEY,NAME) AS(
    SELECT 1, 'C' FROM DUAL UNION ALL
    SELECT 2, 'A' FROM DUAL UNION ALL
    SELECT 3, 'B' FROM DUAL UNION ALL
    SELECT 4, 'A' FROM DUAL UNION ALL
    SELECT 5, 'A' FROM DUAL UNION ALL
    SELECT 6, 'B' FROM DUAL UNION ALL
    SELECT 7, 'C' FROM DUAL UNION ALL
    SELECT 8, 'D' FROM DUAL UNION ALL
    SELECT 9, 'E' FROM DUAL
)
SELECT TB.KEY, TB.NAME
     , CASE WHEN TA.CNT >= ROW_NUMBER() OVER(PARTITION BY TB.NAME ORDER BY TB.KEY)
                 OR TA.NAME IS NULL -- A테이블에 없는 NAME인 경우 승인처리
            THEN '승인'
            ELSE '취소'
            END VAL
FROM (
      SELECT NAME, COUNT(*) CNT
      FROM TA
      GROUP BY NAME
      ) TA
      , TB
WHERE TA.NAME(+) = TB.NAME
ORDER BY 3,1
;

jkson님은 저랑 비슷한 생각을 하셨군요..


by 야신 [2015.07.17 17:10:16]
Select *
, case when rn=1 or a_seq is not null then
'승인'
Else
'취소'
End
From ( select b.*
,count(*) over(partion by b.name order by nullif(b.seq, a.seq) nulls first ) rn
, a.seq as a_seq
From b
, a
Where b.key = a.key( )
)
Order by key
미처 못본 사항이 있어 수정합니다

by 아카시아향기 [2015.07.17 17:21:19]

모두 감사드립니다.

행복한 주말 되세요. ^^

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