Oracle查询百分比基于时间范围

时间:2017-09-12 12:14:48

标签: sql oracle

我有一个Oracle查询来计算以下百分比:

select to_char(ltrim(round((1 - n / c) * 100) || '%')) as total
  from (select count(*) c
          from wa_sew_tbl_emp_info
         where status = 'Attend'
           and shift = case
                  when to_char(sysdate, 'HH24:MI') >= '07:00'
                       and to_char(sysdate, 'HH24:MI') <= '19:29' then
                   'Morning'
                  when to_char(sysdate, 'HH24:MI') >= '19:30'
                       and to_char(sysdate, 'HH24:MI') <= '23:59' then
                   'Night'
                  when to_char(sysdate, 'HH24:MI') >= '00:00'
                       and to_char(sysdate, 'HH24:MI') <= '06:59' then
                   'Night'
               end)
      ,(select count(*) n
         from (select s.badgeid_fk
                 from wa_sew_tbl_emp_info s
                     ,wa_ga_tbl_employees e
                where s.badgeid_fk = e.badgeid
                  and s.status = 'Attend'
                  and s.shift = case
                         when to_char(sysdate, 'HH24:MI') >= '07:00'
                              and to_char(sysdate, 'HH24:MI') <= '19:29' then
                          'Morning'
                         when to_char(sysdate, 'HH24:MI') >= '19:30'
                              and to_char(sysdate, 'HH24:MI') <= '23:59' then
                          'Night'
                         when to_char(sysdate, 'HH24:MI') >= '00:00'
                              and to_char(sysdate, 'HH24:MI') <= '06:59' then
                          'Night'
                      end
                  and s.badgeid_fk not in
                      (select empid
                         from wa_sew_tbl_results
                        where system_date between to_date(case
                                                             when to_char(sysdate, 'HH24:MI') >= '07:00'
                                                                  and to_char(sysdate, 'HH24:MI') <= '19:29' then
                                                              to_char(sysdate, 'YYYY-MM-DD') || ' 07:00'
                                                             when to_char(sysdate, 'HH24:MI') >= '19:30'
                                                                  and to_char(sysdate, 'HH24:MI') <= '23:59' then
                                                              to_char(sysdate, 'YYYY-MM-DD') || ' 19:30'
                                                             when to_char(sysdate, 'HH24:MI') >= '00:00'
                                                                  and to_char(sysdate, 'HH24:MI') <= '06:59' then
                                                              to_char(sysdate, 'YYYY-MM-DD') || ' 00:00'
                                                          end
                                                         ,'YYYY-MM-DD HH24:MI')
                          and to_date(case
                                         when to_char(sysdate, 'HH24:MI') >= '07:00'
                                              and to_char(sysdate, 'HH24:MI') <= '19:29' then
                                          to_char(sysdate, 'YYYY-MM-DD') || ' 19:29'
                                         when to_char(sysdate, 'HH24:MI') >= '19:30'
                                              and to_char(sysdate, 'HH24:MI') <= '23:59' then
                                          to_char(sysdate, 'YYYY-MM-DD') || ' 23:59'
                                         when to_char(sysdate, 'HH24:MI') >= '00:00'
                                              and to_char(sysdate, 'HH24:MI') <= '06:59' then
                                          to_char(sysdate, 'YYYY-MM-DD') || ' 06:59'
                                      end
                                     ,'YYYY-MM-DD HH24:MI'))))

该查询功能是获得百分比。

如上所示,您可以根据今天的日期和时间范围自动设置日期。

日期时间范围查询:
&#39; 07:00&#39; - &#39; 19:29&#39; OK
&#39; 19:30&#39; - &#39; 23:59&#39;行

现在当我们准时:00.00 - 06.59,我想要查询基于:
&#39; 19:30&#39; - &#39; 23:59&#39;和昨天的日期和
&#39; 00:00&#39; - &#39; 06:59&#39;今天约会。

您可以参考此fiddle

1 个答案:

答案 0 :(得分:1)

基于系统日期/时间编写代码的问题是,一旦设置它就会变得多余。我不认为你的sqlfiddle非常有用,因为它返回0%。

另外,你要问的是什么并不清楚。您是否可以使用以下内容来清楚地表达您对下表中显示的每个“s.ysdate”值的期望。请注意,列y,y2,x1,x2是从查询中提取的。

可在SQL Fiddle

处找到
with S as (
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss') as ysdate from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 1/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 2/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 3/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 4/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 5/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 6/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 7/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 8/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+ 9/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+10/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+11/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+12/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+13/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+14/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+15/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+16/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+17/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+18/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+19/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+20/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+21/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+22/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+23/24 from dual union all
        select to_date('2017-10-15 07:01:00','yyyy-mm-dd hh24:mi:ss')+24/24 from dual
          )
SELECT     
   s.ysdate "s.ysdate"
,  case
      when to_char(s.ysdate, 'HH24:MI') >= '07:00'
           and to_char(s.ysdate, 'HH24:MI') <= '19:29' then
       'Morning'
      when to_char(s.ysdate, 'HH24:MI') >= '19:30'
           and to_char(s.ysdate, 'HH24:MI') <= '23:59' then
       'Night'
      when to_char(s.ysdate, 'HH24:MI') >= '00:00'
           and to_char(s.ysdate, 'HH24:MI') <= '06:59' then
       'Night'
  end
  as y
, case
     when to_char(s.ysdate, 'HH24:MI') >= '07:00'
          and to_char(s.ysdate, 'HH24:MI') <= '19:29' then
      'Morning'
     when to_char(s.ysdate, 'HH24:MI') >= '19:30'
          and to_char(s.ysdate, 'HH24:MI') <= '23:59' then
      'Night'
     when to_char(s.ysdate, 'HH24:MI') >= '00:00'
          and to_char(s.ysdate, 'HH24:MI') <= '06:59' then
      'Night'
  end 
  y2
, to_date(case
             when to_char(s.ysdate, 'HH24:MI') >= '07:00'
                  and to_char(s.ysdate, 'HH24:MI') <= '19:29' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 07:00'
             when to_char(s.ysdate, 'HH24:MI') >= '19:30'
                  and to_char(s.ysdate, 'HH24:MI') <= '23:59' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 19:30'
             when to_char(s.ysdate, 'HH24:MI') >= '00:00'
                  and to_char(s.ysdate, 'HH24:MI') <= '06:59' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 00:00'
          end
 ,'YYYY-MM-DD HH24:MI')
 as x1
, to_date(case
             when to_char(s.ysdate, 'HH24:MI') >= '07:00'
                  and to_char(s.ysdate, 'HH24:MI') <= '19:29' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 19:29'
             when to_char(s.ysdate, 'HH24:MI') >= '19:30'
                  and to_char(s.ysdate, 'HH24:MI') <= '23:59' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 23:59'
             when to_char(s.ysdate, 'HH24:MI') >= '00:00'
                  and to_char(s.ysdate, 'HH24:MI') <= '06:59' then
              to_char(s.ysdate, 'YYYY-MM-DD') || ' 06:59'
          end
  ,'YYYY-MM-DD HH24:MI')
  as x2
from s
order by 1

<强> Results

|             s.ysdate |       Y |      Y2 |                   X1 |                   X2 |
|----------------------|---------|---------|----------------------|----------------------|
| 2017-10-15T07:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T08:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T09:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T10:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T11:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T12:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T13:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T14:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T15:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T16:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T17:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T18:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T19:01:00Z | Morning | Morning | 2017-10-15T07:00:00Z | 2017-10-15T19:29:00Z |
| 2017-10-15T20:01:00Z |   Night |   Night | 2017-10-15T19:30:00Z | 2017-10-15T23:59:00Z |
| 2017-10-15T21:01:00Z |   Night |   Night | 2017-10-15T19:30:00Z | 2017-10-15T23:59:00Z |
| 2017-10-15T22:01:00Z |   Night |   Night | 2017-10-15T19:30:00Z | 2017-10-15T23:59:00Z |
| 2017-10-15T23:01:00Z |   Night |   Night | 2017-10-15T19:30:00Z | 2017-10-15T23:59:00Z |
| 2017-10-16T00:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T01:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T02:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T03:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T04:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T05:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T06:01:00Z |   Night |   Night | 2017-10-16T00:00:00Z | 2017-10-16T06:59:00Z |
| 2017-10-16T07:01:00Z | Morning | Morning | 2017-10-16T07:00:00Z | 2017-10-16T19:29:00Z |