[ORACLE] 없는 데이터 강제로 만들기

Database 2014.09.16 11:58 Posted by 애플자라

없는 데이터 강제로 만들기 및 정해진 데이터 표로 리스트 뿌리기

미련하면서도 억지로 만든쿼리

 

SELECT * FROM
(
SELECT SUBSTR(MARKETING_DATE,0,7) AS DAYS, G_CATE, COUNT(G_CATE) AS CNT 
FROM TB_MARKETING   
WHERE SUBSTR(MARKETING_DATE,0,4) = SUBSTR(TO_CHAR(SYSDATE,'YYYYMMDD'),0,4) 
AND G_CATE IN  ('공제회','국가기관','생손보사','은행(중앙회)','금융기관','기타')  
GROUP BY SUBSTR(MARKETING_DATE,0,7),G_CATE
UNION ALL
SELECT DAYS,G_CATE,CNT FROM APLUS_V_TB_MARKETING_ANNUAL
)
ORDER BY DAYS DESC, DECODE(G_CATE,'국가기관',1,'공제회',2,'생손보사',3,'은행(중앙회)',4,'금융기관',5,'기타',6)

 

-----------------------------------------------

APLUS_V_TB_MARKETING_ANNUAL DDL

 

 

CREATE OR REPLACE FORCE VIEW APLUSORA.APLUS_V_TB_MARKETING_ANNUAL
(
   DAYS,
   G_CATE,
   CNT
)
AS
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '공제회' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '공제회'
)
UNION ALL
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '국가기관' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '국가기관'
)
UNION ALL
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '생손보사' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '생손보사'
)
UNION ALL
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '은행(중앙회)' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '은행(중앙회)'
)
UNION ALL
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '금융기관' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '금융기관'
)
UNION ALL
SELECT TO_CHAR(SYSDATE,'YYYY-MM') AS DAYS, '기타' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(SYSDATE,'YYYY-MM') AND G_CATE = '기타'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '공제회' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '공제회'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '국가기관' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '국가기관'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '생손보사' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '생손보사'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '은행(중앙회)' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '은행(중앙회)'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '금융기관' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '금융기관'
)
UNION ALL
SELECT TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AS DAYS, '기타' AS G_CATE, 0 AS CNT FROM DUAL
WHERE 0 =
(
    SELECT COUNT(1) AS CNT FROM TB_MARKETING WHERE SUBSTR(MARKETING_DATE,0,7) = TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYY-MM') AND G_CATE = '기타'
)

 

 

APLUS_V_TB_MARKETING_ANNUAL.SQL

 

 

댓글을 달아 주세요