안녕하세요~ 오라클클럽을 통해 많은 도움을 받고 있는데요^^
B테이블에 있는 KEY값들의 개수를 A테이블 KEY값과 조인하여 A와 같은 KEY의 카운터를 내보려고 합니다.
A테이블 | B테이블 | |||
key | key | soonwi | ||
15471996603396 | 15471996603396 | 1 | ||
15471996617649 | 15471996603396 | 1-1 | ||
15471996603396 | 2 | |||
15471996603396 | 2-1 | |||
15471996603396 | 3 | |||
15471996603396 | 3-1 | |||
15471996617649 | 1 | |||
15471996617649 | 2 | |||
15471996617649 | 3 |
아래와 같이 결과를 내보려고 하는데 문의를 드립니다~^^ 무더위 조심하시고 모기도 조심하시길 바래요~
결과 | |
key | count |
15471996603396 | 6 |
15471996617649 | 3 |
WITH T1 AS ( SELECT 15471996603396 AS key FROM DUAL UNION ALL SELECT 15471996617649 FROM DUAL ), T2 AS ( SELECT 15471996603396 AS key, '1' AS soonwi FROM DUAL UNION ALL SELECT 15471996603396, '1-1' FROM DUAL UNION ALL SELECT 15471996603396, '2' FROM DUAL UNION ALL SELECT 15471996603396, '2-1' FROM DUAL UNION ALL SELECT 15471996603396, '3' FROM DUAL UNION ALL SELECT 15471996603396, '3-1' FROM DUAL UNION ALL SELECT 15471996617649, '1' FROM DUAL UNION ALL SELECT 15471996617649, '2' FROM DUAL UNION ALL SELECT 15471996617649, '3' FROM DUAL ) SELECT key, COUNT(*) FROM T2 WHERE EXISTS(SELECT 1 FROM T1 WHERE key = T2.key) GROUP BY key
WITH T1 AS (
SELECT 15471996603396 AS key FROM DUAL UNION ALL
SELECT 15471996617649 FROM DUAL
), T2 AS (
SELECT 15471996603396 AS key, '1' AS soonwi FROM DUAL UNION ALL
SELECT 15471996603396, '1-1' FROM DUAL UNION ALL
SELECT 15471996603396, '2' FROM DUAL UNION ALL
SELECT 15471996603396, '2-1' FROM DUAL UNION ALL
SELECT 15471996603396, '3' FROM DUAL UNION ALL
SELECT 15471996603396, '3-1' FROM DUAL UNION ALL
SELECT 15471996617649, '1' FROM DUAL UNION ALL
SELECT 15471996617649, '2' FROM DUAL UNION ALL
SELECT 15471996617649, '3' FROM DUAL
)
select t1.key,count(t1.key)cnt from t1,t2
where t1.key=t2.key
group by t1.key
order by t1.key;
SELECT B.VAL1 , COUNT(B.VAL2) FROM (SELECT '15471996603396' VAL1 FROM DUAL UNION ALL SELECT '15471996617649' FROM DUAL) A , (SELECT '15471996603396' VAL1 , 1 VAL2 FROM DUAL UNION ALL SELECT '15471996603396' , 1-1 FROM DUAL UNION ALL SELECT '15471996603396' , 2 FROM DUAL UNION ALL SELECT '15471996603396' , 2-1 FROM DUAL UNION ALL SELECT '15471996603396' , 3 FROM DUAL UNION ALL SELECT '15471996603396' , 3-1 FROM DUAL UNION ALL SELECT '15471996617649' , 1 FROM DUAL UNION ALL SELECT '15471996617649' , 2 FROM DUAL UNION ALL SELECT '15471996617649' , 3 FROM DUAL UNION ALL SELECT '15471996617659' , 1 FROM DUAL ) B WHERE A.VAL1 = B.VAL1 GROUP BY B.VAL1