품질 Q 그림 관건 SQL 연구 - 날짜 데이터 처리


--  :              
--         ,          ,          ,          --


--  UI:  DatePicker,         Q ;      ,  FullCalendar  
--  Q      PCR    ,   Cell  :
----  :    、           
----  :    、               
----  :    、          ;         、      

--        ,  datepicker    (yyyymm)  sql,     “        ”
select distinct replace(t.PUB_DATE, '-') from T_PCR t where replace(substr(t.PUB_DATE, 1, 7), '-') ='201711';

--   :      , PCR  
--     
create table qsb_data_test01
(
       occur_date varchar2(10),
       prob_type  varchar2(10),
       prob_desc  varchar2(100),--     
       grade      varchar2(10)--     
)

--        
insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180101', '  ', '  ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180102', '  ', '  ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180103', '   ', '   ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180104', '  ', '   ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180104', '  ', '  ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20180106', '   ', '    ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20171231', '  ', '  ', '');

insert into qsb_data_test01 (OCCUR_DATE, PROB_TYPE, PROB_DESC, GRADE)
values ('20171230', '   ', '   ', '');
select * from qsb_data_test01 t;

--    :   1 
select to_char(trunc(sysdate, 'mm'),'yyyymmdd') from dual;

--    :  
select to_char(sysdate,'yyyymmdd') from dual; 

--      (  )     
--  DatePicker          ?              ;    ,          ;
--  ,         。
--     :       ,      DatePicker     【      】


--      ,   ,          ;               ,         (  )    
select * from qsb_data_test01 t 
where substr(t.occur_date, 1, 6) ='201801' 
and t.occur_date<=to_char(sysdate,'yyyymmdd');


--      ,      (      -3,  /     -2)  
  select t.occur_date,
         t.prob_type,
         (case t.prob_type
           when '  ' then
            '3'
           when '  ' then
            '2'
           when '   ' then
            '2'
         end) as   
    from qsb_data_test01 t;
    
--        ,              
  select t.occur_date as       ,
         max(case t.prob_type
               when '  ' then
                '3'
               when '  ' then
                '2'
               when '   ' then
                '2'
             end) as   
    from qsb_data_test01 t
   group by t.occur_date
   order by t.occur_date;
   
--        ,              ;             
   select cc.occur_date,
          (case cc.grade
            when '3' then
             'red'
            when '2' then
             'yello'
          end) q_color
     from (select t.occur_date,
                  max(case t.prob_type
                        when '  ' then
                         '3'
                        when '  ' then
                         '2'
                        when '   ' then
                         '2'
                      end) as grade
             from qsb_data_test01 t
            where substr(t.occur_date, 1, 6) = '201712'
              and t.occur_date <= to_char(sysdate, 'yyyymmdd') --      
            group by t.occur_date
            order by t.occur_date) cc


--                
--   ,         (  SQL):              ,               ,       






--       ,       :
----1.      ,           , :  ==    ,  <=  
----2.                 
----3.      ,    、      ,       (        )
----4.      ,       ,       (        )
----5.     FullCalendar,     Events   ,        


--                 

--    :Oracle              
SELECT TO_DATE('2017-01-01', 'yyyy-MM-dd') + ROWNUM - 1 as daylists
  FROM DUAL
CONNECT BY ROWNUM <=
           trunc( to_date('2017-12-31', 'yyyy-MM-dd') -     --    
                  to_date('2017-01-01', 'yyyy-MM-dd')       --    
                ) + 1; 
                 

--    :    (    )X ,     X=60,   60   
SELECT trunc(sysdate, 'dd') + ROWNUM - 60 as daylists
  FROM DUAL
CONNECT BY ROWNUM <=
           trunc(trunc(sysdate, 'dd') - (trunc(sysdate, 'dd') - 60));    
                
--        ,         ,           
select t1.daylists
  from (SELECT to_char(trunc(sysdate, 'dd') + ROWNUM - 30, 'yyyymmdd') as daylists
          FROM DUAL
        CONNECT BY ROWNUM <=
                   trunc(trunc(sysdate, 'dd') - (trunc(sysdate, 'dd') - 30))) t1
 where t1.daylists not in (select t.occur_date from qsb_data_test01 t)
 order by t1.daylists;

--    : not exist   not in
select t1.daylists
  from (SELECT to_char(trunc(sysdate, 'dd') + ROWNUM - 30, 'yyyymmdd') as daylists
          FROM DUAL
        CONNECT BY ROWNUM <=
                   trunc(trunc(sysdate, 'dd') - (trunc(sysdate, 'dd') - 30))) t1
 where not exists (select t.occur_date
          from qsb_data_test01 t
         where t.occur_date = t1.daylists)
 order by t1.daylists;

--------------------------------END.

좋은 웹페이지 즐겨찾기