安排日期查询结果[oracle 11g]

时间:2016-05-24 15:04:20

标签: oracle date oracle11g dual-table

这是我根据天数获取两个日期之间所有可能日期的查询。

    select A.presentationID,
       A.PRESENTATIONDAY,
       TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN') LIST_DATE
from 
  PRESENTATION A, 
  (
     select level-1 as delta 
     from dual 
     connect by level-1 <= (
       select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
     )
  )
where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
and
  a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))
order by 1,2,3;

presentation表中检索值,其中包含presentationdaypresentationdatestartpresentationdateend

此查询的结果是:

622 Monday  02-05-2016 12:00:00
622 Monday  09-05-2016 12:00:00
622 Monday  16-05-2016 12:00:00
622 Monday  23-05-2016 12:00:00
622 Monday  30-05-2016 12:00:00
623 Tuesday 03-05-2016 12:00:00
623 Tuesday 10-05-2016 12:00:00
623 Tuesday 17-05-2016 12:00:00
623 Tuesday 24-05-2016 12:00:00
623 Tuesday 31-05-2016 12:00:00
624 Wednesday   04-05-2016 12:00:00
624 Wednesday   11-05-2016 12:00:00
624 Wednesday   18-05-2016 12:00:00
624 Wednesday   25-05-2016 12:00:00
624 Wednesday   01-06-2016 12:00:00
625 Thursday    05-05-2016 12:00:00
625 Thursday    12-05-2016 12:00:00
625 Thursday    19-05-2016 12:00:00
625 Thursday    26-05-2016 12:00:00
625 Thursday    02-06-2016 12:00:00

我如何将这些值安排成这样的东西:

    622 Monday      02-05-2016     
    623 Tuesday     03-05-2016     
    624 Wednesday   04-05-2016     
    625 Thursday    05-05-2016     
    622 Monday      09-05-2016     
    623 Tuesday     10-05-2016  
    624 Wednesday   11-05-2016    
    625 Thursday    12-05-2016 
    622 Monday      16-05-2016 
    ....
    625 Thursday    02-06-2016 

2 个答案:

答案 0 :(得分:1)

使用此:

TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN' ) LIST_DATE

而不是

A.PRESENTATIONDATESTART+delta LIST_DATE

这会格式化您的日期

新的挑战

更新回答

select * from (
  select A.presentationID,
       A.PRESENTATIONDAY,


          TO_CHAR(A.PRESENTATIONDATESTART+delta,'DD-MM-YYYY','NLS_CALENDAR=GREGORIAN') LIST_DATE,
    row_number() over (partition by presentationID,PRESENTATIONDATESTART+delta
order by presentationID,PRESENTATIONDATESTART+delta) r
    from 
      PRESENTATION A, 
      (
         select level-1 as delta 
         from dual 
         connect by level-1 <= (
           select max(PRESENTATIONDATEEND- PRESENTATIONDATESTART) from PRESENTATION
         )
      )
    where A.PRESENTATIONDATESTART+delta <= A.PRESENTATIONDATEEND
    and
      a.presentationday = trim(to_char(A.PRESENTATIONDATESTART+delta, 'Day'))

    )
    order by r

答案 1 :(得分:1)

我想你就是在这之后:

select   a.presentationid,
         a.presentationday,
         to_char (a.presentationdatestart + delta, 'DD-MM-YYYY', 'NLS_CALENDAR=GREGORIAN') list_date
from     presentation a,
         (select     level - 1 as delta
          from       dual
          connect by level - 1 <= (select max (presentationdateend - presentationdatestart)
                                   from   presentation))
where    a.presentationdatestart + delta <= a.presentationdateend
and      a.presentationday = to_char(a.presentationdatestart + delta, 'fmDay')
order by a.presentationdatestart + delta,
         a.presentationid;

N.B。请注意我是如何删除trim()并将其替换为格式掩码中的fm

P.S。您可以通过这样做来重写查询以删除连接条件(以及对表示表的额外调用):

with presentation as (select 622 presentationid, 'Monday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('31/05/2016', 'dd/mm/yyyy') presentationdateend from dual union all
                      select 623 presentationid, 'Tuesday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('31/05/2016', 'dd/mm/yyyy') presentationdateend from dual union all
                      select 624 presentationid, 'Wednesday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('07/06/2016', 'dd/mm/yyyy') presentationdateend from dual union all
                      select 625 presentationid, 'Thursday' presentationday, to_date('01/05/2016', 'dd/mm/yyyy') presentationdatestart, to_date('07/06/2016', 'dd/mm/yyyy') presentationdateend from dual)
-- end of mimicking your presentation table with data in it. You wouldn't need this subquery as you have the table; see SQL below.
select presentationid,
       presentationday,
       to_char(next_day(presentationdatestart -1, presentationday) + 7*(level - 1), 'DD-MM-YYYY') list_date
from   presentation
connect by prior presentationid = presentationid
           and prior sys_guid() is not null
           and next_day(presentationdatestart -1, presentationday) + 7*(level - 1) <= presentationdateend
order by next_day(presentationdatestart -1, presentationday) + 7*(level - 1),
         presentationid;

PRESENTATIONID PRESENTATIONDAY LIST_DATE 
-------------- --------------- ----------
           622 Monday          02-05-2016
           623 Tuesday         03-05-2016
           624 Wednesday       04-05-2016
           625 Thursday        05-05-2016
           622 Monday          09-05-2016
           623 Tuesday         10-05-2016
           624 Wednesday       11-05-2016
           625 Thursday        12-05-2016
           622 Monday          16-05-2016
           623 Tuesday         17-05-2016
           624 Wednesday       18-05-2016
           625 Thursday        19-05-2016
           622 Monday          23-05-2016
           623 Tuesday         24-05-2016
           624 Wednesday       25-05-2016
           625 Thursday        26-05-2016
           622 Monday          30-05-2016
           623 Tuesday         31-05-2016
           624 Wednesday       01-06-2016
           625 Thursday        02-06-2016