sql - ORACLE Query Percentage Based On Time Range -
i have oracle query count percentage below:
select to_char(ltrim(round((1 - n / c) * 100) || '%')) total (select count(*) c wa_sew_tbl_emp_info status = 'attend' , shift = case when to_char(sysdate, 'hh24:mi') >= '07:00' , to_char(sysdate, 'hh24:mi') <= '19:29' 'morning' when to_char(sysdate, 'hh24:mi') >= '19:30' , to_char(sysdate, 'hh24:mi') <= '23:59' 'night' when to_char(sysdate, 'hh24:mi') >= '00:00' , to_char(sysdate, 'hh24:mi') <= '06:59' 'night' end) ,(select count(*) n (select s.badgeid_fk wa_sew_tbl_emp_info s ,wa_ga_tbl_employees e s.badgeid_fk = e.badgeid , s.status = 'attend' , s.shift = case when to_char(sysdate, 'hh24:mi') >= '07:00' , to_char(sysdate, 'hh24:mi') <= '19:29' 'morning' when to_char(sysdate, 'hh24:mi') >= '19:30' , to_char(sysdate, 'hh24:mi') <= '23:59' 'night' when to_char(sysdate, 'hh24:mi') >= '00:00' , to_char(sysdate, 'hh24:mi') <= '06:59' 'night' end , s.badgeid_fk not in (select empid wa_sew_tbl_results system_date between to_date(case when to_char(sysdate, 'hh24:mi') >= '07:00' , to_char(sysdate, 'hh24:mi') <= '19:29' to_char(sysdate, 'yyyy-mm-dd') || ' 07:00' when to_char(sysdate, 'hh24:mi') >= '19:30' , to_char(sysdate, 'hh24:mi') <= '23:59' to_char(sysdate, 'yyyy-mm-dd') || ' 19:30' when to_char(sysdate, 'hh24:mi') >= '00:00' , to_char(sysdate, 'hh24:mi') <= '06:59' to_char(sysdate, 'yyyy-mm-dd') || ' 00:00' end ,'yyyy-mm-dd hh24:mi') , to_date(case when to_char(sysdate, 'hh24:mi') >= '07:00' , to_char(sysdate, 'hh24:mi') <= '19:29' to_char(sysdate, 'yyyy-mm-dd') || ' 19:29' when to_char(sysdate, 'hh24:mi') >= '19:30' , to_char(sysdate, 'hh24:mi') <= '23:59' to_char(sysdate, 'yyyy-mm-dd') || ' 23:59' when to_char(sysdate, 'hh24:mi') >= '00:00' , to_char(sysdate, 'hh24:mi') <= '06:59' to_char(sysdate, 'yyyy-mm-dd') || ' 06:59' end ,'yyyy-mm-dd hh24:mi'))))
that query function percentage.
as can see above code, date automatically set based on today date , time range.
the date time range query:
'07:00' - '19:29' ok
'19:30' - '23:59' ok
and when on time: 00.00 - 06.59, want query based on:
'19:30' - '23:59' date yesterday and
'00:00' - '06:59' date today.
you can refer fiddle
Comments
Post a Comment