每天使用最小和最大时差在选择查询中创建重复记录

时间:2015-11-05 06:58:18

标签: sql oracle plsql

最近我遇到的情况是,如果记录的最小和最大日期不在同一个日期,我想在不同的日期显示一条记录,到目前为止我已经尝试了这两个查询但是一个查询需要很长时间和另一个查询不显示所有记录。两个查询都是。 查询1(花费很长时间):

select ((b.starttime+b.srlno) -1 ) datevals, to_char(((b.starttime+b.srlno) -1 ),'mm/dd/yyyy') dateval,
   extract(YEAR FROM (b.starttime+b.srlno)-1 ) eyear,
   extract(MONTH FROM (b.starttime+b.srlno)-1 )-1 emonth,
   extract(day from (b.starttime+b.srlno)-1 )-0 eday,
   b.* from  (select   ROW_NUMBER( ) OVER (PARTITION BY
                                              a.id ORDER BY a.starttime NULLS LAST) SRLNO , 
          a.* from  (select  rr.id, rr.title ,min(rrd.starttime) starttime,
                     max(rrd.endtime) endtime,
                     to_char(min(rrd.starttime),'mm/dd/yyyy') startDate,
                     to_char(min(rrd.starttime),'fmHH:fmMI am') displaytime
                    -- JavaRoomBookingAPI.getCisnumber(rr.event_id) cisNumber
                   --  JavaRoomBookingAPI.getroomName(rr.id,rrd.room_id) roomName
              from roomreservation rr, roomreservationdetails  rrd,room r
              Where  rrd.roomreservation_id = rr.id 
                      and rrd.room_id= r.id and  rr.CANCELFLAG <> 1 
                      and  nvl(rrd.setuptime, rrd.starttime) != nvl(rrd.breakdowntime, rrd.endtime) 
                      and  rrd.starttime != rrd.endtime 
              group by    rr.id, rr.title
                        --JavaRoomBookingAPI.getCisnumber(rr.event_id)
                     --  JavaRoomBookingAPI.getroomName(rr.id,rrd.room_id)
              having   To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') BETWEEN min(RRD.STARTTIME ) and max(RRD.Endtime) 
                       or 
                       To_Date('11/30/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') BETWEEN min(RRD.STARTTIME )  and max(RRD.Endtime) 
                       or 
                       min(RRD.STARTTIME ) BETWEEN  To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') and   To_Date('11/30/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') 
                       or 
                       max(RRD.ENDTIME ) BETWEEN  To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') and   To_Date('11/30/2019 12:00 AM','MM/DD/YYYY HH:Mi AM') 

            order by rr.id
            )a,
            (SELECT rownum repeat FROM dual
                   CONNECT BY LEVEL<= (select ceil(trunc(max(endtime))-trunc(min(starttime))) from roomreservationdetails)
            ) r
            where ceil(trunc(a.endtime)-trunc(a.starttime))+1 >= r.repeat 
            order by a.starttime,a.id) b order by datevals, b.title;

和2查询是(不显示所有记录):

select ((b.starttime+b.srlno) -1 ) datevals, to_char(((b.starttime+b.srlno) -1 ),'mm/dd/yyyy') dateval,
                   extract(YEAR FROM (b.starttime+b.srlno)-1 ) eyear,
                   extract(MONTH FROM (b.starttime+b.srlno)-1 )-1 emonth,
                   extract(day from (b.starttime+b.srlno)-1 )-0 eday,
                  b.* from  
                  (select   ROW_NUMBER( ) OVER (PARTITION BY
                                                  a.id ORDER BY a.starttime NULLS LAST) SRLNO , a.* from 
                    (select  rr.id, rr.title ,min(rrd.starttime) starttime,
                    max(rrd.endtime) endtime,
                   to_char(min(rrd.starttime),'mm/dd/yyyy') startDate,
                   to_char(min(rrd.starttime),'fmHH:fmMI am') displaytime,
                   JavaRoomBookingAPI.getCisnumber(rr.event_id) cisNumber,
                   JavaRoomBookingAPI.getroomName(rr.id,rrd.room_id) roomName

         from roomreservation rr, roomreservationdetails  rrd,room r
         Where  rrd.roomreservation_id = rr.id 
                      and rrd.room_id= r.id and  rr.CANCELFLAG <> 1 and                
                   (
                   decode(to_char(rrd.starttime,'hh:mi am'),'12:00 am',rrd.starttime + 1/1440,rrd.starttime) between 
                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) And
                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM')) 
                         or 
                   decode(to_char(rrd.endtime,'hh:mi am'),'12:00 pm',rrd.endtime - 1/1440,rrd.endtime) between 
                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) And
                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM'))
                   or 
                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) between 
                    decode(to_char(rrd.starttime,'hh:mi am'),'12:00 pm',rrd.starttime + 1/1440,rrd.starttime) 
                     And
                    decode(to_char(rrd.endtime,'hh:mi am'),'12:00 pm',rrd.endtime - 1/1440,rrd.endtime)
                   or 
                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM')) between 
                    decode(to_char(rrd.starttime,'hh:mi am'),'12:00 pm',rrd.starttime + 1/1440,rrd.starttime) 
                     And
                    decode(to_char(rrd.endtime,'hh:mi am'),'12:00 pm',rrd.endtime - 1/1440,rrd.endtime)
                  ) 
                 and  nvl(rrd.setuptime, rrd.starttime) != nvl(rrd.breakdowntime, rrd.endtime) 
                 and  rrd.starttime != rrd.endtime 
                 group by     rr.id, rr.title,JavaRoomBookingAPI.getCisnumber(rr.event_id),JavaRoomBookingAPI.getroomName(rr.id,rrd.room_id)
                 order by rr.id) a,
                               (SELECT rownum repeat FROM dual
                CONNECT BY LEVEL<= (select ceil(trunc(max(endtime))-trunc(min(starttime))) from roomreservationdetails
                                    where (decode(to_char(starttime,'hh:mi am'),'12:00 am',starttime + 1/1440,starttime) between 
                                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) And
                                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM')) or 

                                   decode(to_char(endtime,'hh:mi am'),'12:00 pm',endtime - 1/1440,endtime) between 
                                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) And
                                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM'))

                                   or 

                                   (To_Date('11/01/2019 12:00 AM','MM/DD/YYYY HH:Mi AM')) between 
                                    decode(to_char(starttime,'hh:mi am'),'12:00 pm',starttime + 1/1440,starttime) 
                                     And
                                    decode(to_char(endtime,'hh:mi am'),'12:00 pm',endtime - 1/1440,endtime)

                                   or 

                                   (To_Date('11/30/2019 11:59 PM','MM/DD/YYYY HH:Mi AM')) between 
                                    decode(to_char(starttime,'hh:mi am'),'12:00 pm',starttime + 1/1440,starttime) 
                                     And
                                    decode(to_char(endtime,'hh:mi am'),'12:00 pm',endtime - 1/1440,endtime))
                )) r
                where ceil(trunc(a.endtime)-trunc(a.starttime))+1 >= r.repeat 
                order by a.starttime,a.id) b order by datevals, b.title;

0 个答案:

没有答案