본문 바로가기

IT/PostgresSQL

휴일관리 SQL 구성

/* selectHoliday */
SELECT a.YYYYMM                                                                                         AS YY       /*년월*/
              ,  a.YYYYMM || b.DT                                                                           AS YMD  /*년월일*/
              , b.DT                                                                                                                           /*일자*/
              , to_char(to_date(a.YYYYMM || b.DT, 'yyyymmdd'), 'd')   AS DOTW_CLSF_CD    /*요일(1-일, 2-월, 3-화, 4-수, 5-목, 6-금, 7-토)*/
              , CASE WHEN c.DT IS NOT NULL THEN 'Y'
                             WHEN to_char(to_date(a.YYYYMM || b.DT, 'yyyymmdd'), 'd') IN ('1', '7') THEN 'Y'
                             ELSE 'N' END                                                                        AS HLDY_YN /*휴일여부*/
              , c.BS_DT_NM
              , to_char(COALESCE(c.CHNG_DTTM, c.REG_DTTM), 'YYYY-MM-DD HH24:MI')              AS CHNG_DTTM /*수정일시*/
              ,COALESCE(c.CHNG_USER_ID, c.RGNT_ID)                         AS CHNG_USER_ID                /*수정자*/
FROM (
                SELECT to_date(to_char(date( #{yearMm} '01') + interval'31 days', 'yyyymm') || '01', 'yyyymmdd') + interval '-1 days' AS MAX_DD
                             , #{yearMm}                                                                                                                                                                                                            AS YYYYMM                             
              ) a
              INNER JOIN
              (
                select '01' as DT union all
                select '02' as DT union all
                select '03' as DT union all
                select '04' as DT union all
                select '05' as DT union all
                select '06' as DT union all
                select '07' as DT union all
                select '08' as DT union all
                select '09' as DT union all
                select '10' as DT union all
                select '11' as DT union all
                select '12' as DT union all
                select '13' as DT union all
                select '14' as DT union all
                select '15' as DT union all
                select '16' as DT union all
                select '17' as DT union all
                select '18' as DT union all
                select '19' as DT union all
                select '20' as DT union all
                select '21' as DT union all
                select '22' as DT union all
                select '23' as DT union all
                select '24' as DT union all
                select '25' as DT union all
                select '26' as DT union all
                select '27' as DT union all
                select '28' as DT union all
                select '29' as DT union all
                select '30' as DT union all
                select '31' as DT union all
                ) b
                    on 1=1
               left outer join DTJM1023 c
                  on c.DT            = a.YYYYMM || b.DT
                and c.HLDY_YN ='Y'
        where 1=1
              and to_char(a.MAX_DD, 'dd) >= b.DT
       order by 2


a --> 현재년월(YYYYMM), 현재년월의 마지막날짜(30일? 31일? 2월이면 28,29? check)

to_date(A, B)

b -->


c --> 휴일관리테이블(DTJM1023)
      날짜와 휴일유무 를 대조