[ORACLE]WMSYS.WM_CONCAT 월별데이터 합치기

Database 2018.04.06 09:17 Posted by 애플자라

SELECT 
    GROUP_NAME,
    WMSYS.WM_CONCAT(MARKETING_DATE_01) AS DATE_01,
    WMSYS.WM_CONCAT(MARKETING_DATE_02) AS DATE_02,
    WMSYS.WM_CONCAT(MARKETING_DATE_03) AS DATE_03,
    WMSYS.WM_CONCAT(MARKETING_DATE_04) AS DATE_04,
    WMSYS.WM_CONCAT(MARKETING_DATE_05) AS DATE_05,
    WMSYS.WM_CONCAT(MARKETING_DATE_06) AS DATE_06,
    WMSYS.WM_CONCAT(MARKETING_DATE_07) AS DATE_07,
    WMSYS.WM_CONCAT(MARKETING_DATE_08) AS DATE_08,
    WMSYS.WM_CONCAT(MARKETING_DATE_09) AS DATE_09,
    WMSYS.WM_CONCAT(MARKETING_DATE_10) AS DATE_10,
    WMSYS.WM_CONCAT(MARKETING_DATE_11) AS DATE_11,
    WMSYS.WM_CONCAT(MARKETING_DATE_12) AS DATE_12
FROM (SELECT GROUP_NAME,
       MARKETING_DATE,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '01' THEN MARKETING_DATE END MARKETING_DATE_01,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '02' THEN MARKETING_DATE END MARKETING_DATE_02,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '03' THEN MARKETING_DATE END MARKETING_DATE_03,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '04' THEN MARKETING_DATE END MARKETING_DATE_04,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '05' THEN MARKETING_DATE END MARKETING_DATE_05,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '06' THEN MARKETING_DATE END MARKETING_DATE_06,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '07' THEN MARKETING_DATE END MARKETING_DATE_07,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '08' THEN MARKETING_DATE END MARKETING_DATE_08,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '09' THEN MARKETING_DATE END MARKETING_DATE_09,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '10' THEN MARKETING_DATE END MARKETING_DATE_10,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '11' THEN MARKETING_DATE END MARKETING_DATE_11,
       CASE WHEN SUBSTR (MARKETING_DATE, 6, 2) = '12' THEN MARKETING_DATE END MARKETING_DATE_12,
       ROW_NUMBER () OVER (PARTITION BY GROUP_NAME ORDER BY MARKETING_DATE) RNUM
  FROM TB_MARKETING
 WHERE     SUBSTR (MARKETING_DATE, 0, 4) = '2018'
       --AND GROUP_NAME = '건설기술용역공제조합' 
     )
START WITH RNUM = 1
CONNECT BY PRIOR RNUM = RNUM - 1 AND PRIOR GROUP_NAME = GROUP_NAME
GROUP BY GROUP_NAME
ORDER BY GROUP_NAME ASC

 

출처 - http://blog.naver.com/PostView.nhn?blogId=jaejin1028&logNo=220793468335 

 

[SKT]일본로밍서비스정보

Board 2017.12.01 10:54 Posted by 애플자라

 

'Board' 카테고리의 다른 글

[직구]Shirts 사이즈  (0) 2018.03.09
[SKT]일본로밍서비스정보  (0) 2017.12.01
[명함관리]REMEMBER  (0) 2017.10.26
[EXCEL]SUMIF, SUMIFS  (0) 2017.10.18
[민원24]주민등록표등본교부신청  (0) 2017.10.12
[직구]FTA관세  (0) 2017.09.08

[ORACLE]그룹함수로 중복된 컬럼 제거하기

Database 2017.11.08 15:16 Posted by 애플자라

-- 2건이상 데이터
SELECT  MAX(ID) AS ID, MAX(TIMS_DATE) AS TIMS_DATE, MAX(FUND_CODE) AS FUND_CODE, COUNT(*) AS CNT
  FROM IFT_FUND_STANDARD_MODIFY
  WHERE FUND_CODE ='01032'
GROUP BY TIMS_DATE||FUND_CODE
HAVING COUNT(*) > 1

 

-- 2건이상 데이터중 과거 데이터 삭제
DELETE  -- SELECT꼭 해보고 날려버린다
FROM IFT_FUND_STANDARD_MODIFY
WHERE TIMS_DATE||FUND_CODE
IN
(
    SELECT TIMS_DATE||FUND_CODE
    FROM IFT_FUND_STANDARD_MODIFY
    WHERE FUND_CODE ='01032'
    GROUP BY TIMS_DATE||FUND_CODE
    HAVING COUNT(*) > 1
)
AND ID NOT IN (
SELECT  MAX(ID) as ID
  FROM IFT_FUND_STANDARD_MODIFY
  WHERE FUND_CODE ='01032'
GROUP BY TIMS_DATE||FUND_CODE
HAVING COUNT(*) > 1
)

 

CONCAT(TIMS_DATE,FUND_CODE) 도 가능

 

출처 - http://linguist79.tistory.com/49

--  1,2 컬럼값이 서로 같고 3컬럼값만 다른경우
SELECT * FROM A, B
WHERE (A.1 = B.1 AND A.2 = B.2)
AND A.3 <> B.3;

--  1,3컬럼값은 같고  2컬럼값만 다른 경우
SELECT * FROM A, B
WHERE (A.1 = B.1 AND A.3 = B.3)
AND A.2 <> B.2;

--  2,3컬럼값은 같고  1컬럼값만 다른 경우
SELECT * FROM A, B
WHERE (A.2 = B.2 AND A.3 = B.3)
AND A.1 <> B.1;


--  1,2,3 컬럼이 전부 다른 경우
SELECT * FROM A, B
WHERE A.1 <> B.1 AND A.2 <> B.2
AND A.3 <> B.3;

 

출처 - http://www.gurubee.net/article/59245

 

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

 

SELECT A.TIMS_DATE,
       A.FUND_CODE,
       A.FUND_NAME,
       A.BM_INDEX,
       B.BM_INDEX,
       A.BM_RATE,
       B.BM_RATE
  FROM IFT_FUND_STANDARD_PRICE_RESULT A, IFT_FUND_STANDARD_MODIFY B
 WHERE     (A.BM_INDEX <> B.BM_INDEX OR A.BM_RATE <> B.BM_RATE)
       --AND A.TIMS_DATE = '20171104'
       AND A.TIMS_DATE = TO_CHAR(SYSDATE-3, 'YYYYMMDD')
       AND A.TIMS_DATE = B.TIMS_DATE
       AND A.FUND_CODE = B.FUND_CODE

[ORACLE]CLOB DATA Export Excel(DBMS_LOB.SUBSTR)

Database 2016.03.28 15:09 Posted by 애플자라

CLOB 데이터가 원하는데로 안나와서 일단 미련하게 추출; 


 SELECT 

   BOARD_SEQ,

         SEQ,

         TITLE,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 1)  AS CONTENTS1,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 2001)     AS CONTENTS2,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 4001)     AS CONTENTS3,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 6001)     AS CONTENTS4,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 8001)     AS CONTENTS5,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 10001)    AS CONTENTS6,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 12001)    AS CONTENTS7,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 14001)    AS CONTENTS8,

         DBMS_LOB.SUBSTR(CNTNT, 2000, 16001)    AS CONTENTS9

         --dbms_lob.getlength(CNTNT) -- 15801

    FROM TBD_BOARD

   WHERE BOARD_SEQ = '3'

ORDER BY SEQ DESC

* 한 컬럼 중복제거 후 이어 붙이기

 

SELECT LAW_ID,

       SUBSTR(XMLAGG(SYS.XMLTYPE.CREATEXML('<'||FLAG||'>,' || CONTRACT_GESTALT||'</'||FLAG||'>') ORDER BY CONTRACT_GESTALT).EXTRACT('/a1/text()'), 2) CONTRACT_GESTALT,

       SUBSTR(XMLAGG(SYS.XMLTYPE.CREATEXML('<'||FLAG||'>,' || MEMO1||'</'||FLAG||'>') ORDER BY MEMO1).EXTRACT('/a1/text()'), 2) MEMO1

 FROM (

       SELECT

           LAW_ID,

           CONTRACT_GESTALT,NVL2(CONTRACT_GESTALT, 'a' || ROW_NUMBER() OVER (PARTITION BY LAW_ID, CONTRACT_GESTALT ORDER BY CONTRACT_GESTALT),

           'a2')  AS FLAG,

           MEMO1,NVL2(MEMO1, 'a' || ROW_NUMBER() OVER (PARTITION BY LAW_ID, MEMO1 ORDER BY MEMO1),

           'a3')  AS FLAG1

       FROM LAW_OP_FUND)      

GROUP BY LAW_ID

 

* 중복제거 없이 이어 붙이기

 

SELECT 

    LAW_ID,

    SUBSTR (MAX (SYS_CONNECT_BY_PATH (CONTRACT_GESTALT, ',')), 2) CONTRACT_GESTALT

FROM (SELECT LAW_ID, CONTRACT_GESTALT,

       ROW_NUMBER () OVER (PARTITION BY LAW_ID ORDER BY CONTRACT_GESTALT) rnum

       FROM LAW_OP_FUND)

START WITH rnum = 1

CONNECT BY PRIOR rnum = rnum - 1 AND PRIOR LAW_ID = LAW_ID

GROUP BY LAW_ID

 

또는

 

SELECT

    LAW_ID,

    MAX(LTRIM(SYS_CONNECT_BY_PATH(CONTRACT_GESTALT,','),',')) AS CONTRACT_GESTALT

FROM

    (

    SELECT LAW_ID,CONTRACT_GESTALT,ROW_NUMBER() OVER(PARTITION BY LAW_ID ORDER BY ROWNUM) AS RN

    FROM LAW_OP_FUND T1

    --where LAW_ID ='2014091700002'

    )

START WITH RN = 1

CONNECT BY PRIOR LAW_ID = LAW_ID AND PRIOR RN = RN - 1

GROUP BY LAW_ID

ORDER BY LAW_ID ASC

 

출처 - http://amnesis.tistory.com/13

[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

 

 

1. 데이터를 옮길 대상 테이블을 생성한다. : "TEMP_20130607" 라하자.;

 

CREATE TABLE TEMP_20130607
(
  TXDATE               VARCHAR2(8 BYTE)         NOT NULL,
  WRAP_ACCOUNT_NO      VARCHAR2(20 BYTE)        NOT NULL,
  WRAP_ACCOUNT_AMOUNT  VARCHAR2(4000 BYTE)
)
TABLESPACE APLUS_DATA_TS
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
           )
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;

 

2. FUNCTION 생성

 

CREATE OR REPLACE FUNCTION APLUSORA.FNC_LONG2CHAR (  
    ori_rowid        ROWID,  
    ori_column       VARCHAR2,
    ori_table        VARCHAR2  
)  
    RETURN VARCHAR
AS  
    longCont   VARCHAR2 (32767);  
    sqlQuery   VARCHAR2 (2000);
BEGIN  
    sqlQuery :=         'select ' || ori_column || ' from '|| ori_table ||         ' where rowid = '|| CHR (39)|| ori_rowid|| CHR (39);    EXECUTE IMMEDIATE sqlQuery                INTO longCont;        longCont := SUBSTR (longCont, 1, 4000);   RETURN longCont;END;
/

 

3. 실제 사용 쿼리

INSERT INTO TEMP_20130607
(TXDATE,WRAP_ACCOUNT_NO,WRAP_ACCOUNT_AMOUNT)
(SELECT TXDATE,WRAP_ACCOUNT_NO,FNC_LONG2CHAR(ROWID,'WRAP_ACCOUNT_AMOUNT','IFK_CUSTOMER') FROM IFK_CUSTOMER
WHERE  TXDATE = (SELECT MAX(TXDATE) DT FROM IFK_CUSTOMER)
AND     ACCOUNT_STATUS_DESC = '활동')

 

[ORACLE]데이터 페이징 처리방법(게시판)

Database 2012.10.11 17:00 Posted by 애플자라
SELECT B.*
FROM ( SELECT ROW_NUMBER() OVER(ORDER BY DATE DESC) RNUM,A.*
            FROM TABLE A ) B
WHERE RNUM BETWEEN 1 AND 50

 테이블 복사하기 스키마 데이터

CREATE TABLE 새로만들테이블명 AS

SELECT * FROM 복사할테이블명 [WHERE 절]

 

 테이블 구조만 복사하기

CREATE TABLE 새로만들테이블명 AS

SELECT * FROM 복사할테이블명 WHERE 1=2 [where에다가 참이 아닌 조건을 넣어줌]

 

 테이블은 이미 생성되어 있고 데이터만 복사

INSERT INTO 복사할테이블명 SELECT * FROM 복사할테이블명 [WHERE 절]

 

테이블 이름 변경

ALTER TABLE 구테이블명 RENAME TO 신테이블명

주의할 점은, Primary Key 나 Index 등 Constraint 는 복제되지 않습니다. 당연한 것이, 복제된다면 동일한 이름의 Object 가 이미 존재한다는 에러를 만나게 되겠죠. 데이터를 잠시 옮겨놓기 위한 용도의 테이블 복제에선 불필요하다고 봅니다.