如何编写此Oracle查询(上次每个日期的时间<给定时间)

时间:2013-04-03 09:23:59

标签: sql oracle

我正在寻找为Oracle DB编写查询的正确方法。

应该为每个唯一日期(dateCol列)选择最后一行,以便dateCol2的时间部分低于'17:30'

select * from table where dateCol between to_date('2013-01-01 00:00:00','YYYY-MM-DD H24:MI:SS') and to_date('2013-02-01 00:00:00','YYYY-MM-DD H24:MI:SS') 
and id='myID' 
and [MISSING PART = the last line such that timepart(dateCol2)<'17:30']

我是Oracle / SQL的新手,所以我的问题可能会遗漏很多东西,我会在任何请求中添加任何内容。

编辑:到目前为止,我理解这一点:

  select * from (select table.*, row_number() over (order by dateCol2 desc) last_row from table
  where dateCol between to_date('2013-01-01 00:00:00','YYYY-MM-DD H24:MI:SS') 
                    and to_date('2013-02-01 00:00:00','YYYY-MM-DD H24:MI:SS') 
                    and id='myID' 
                    and to_char(dateCol2, 'hh24:mi') < '17:30')
  where last_row = 1
  group by dateCol
  )

4 个答案:

答案 0 :(得分:2)

如果您只将日期存储在datecol中(即当您插入TRUNC日期时),则不需要。如果你也存储非零时间,那么中间也需要调整。

要检查时间,只需将日期转换为hh24mi形式的数字(结果为0到2359之间的数字)并检查小于1730的数字

如果datecol的日期为非00:00:00时间元素:

select distinct dateCol 
  from table 
 where dateCol >= to_date('2013-01-01','YYYY-MM-DD') 
   and dateCol < to_date('2013-01-01','YYYY-MM-DD') + 1
   and id='myID' 
   and to_number(to_char(dateCol2, 'hh24mi')) < 1730;

如果datecol的日期始终为00:00:00,那么:

select distinct dateCol 
  from table 
 where dateCol = to_date('2013-01-01','YYYY-MM-DD') 
   and id='myID' 
   and to_number(to_char(dateCol2, 'hh24mi')) < 1730;

从澄清编辑

select *
  from (select t.*, row_number() over (partition by datecol 
                                       order by dateCol2) first_row,
               row_number() over (partition by datecol 
                                  order by dateCol2 desc) last_row
          from table t
         where dateCol = to_date('2013-01-01','YYYY-MM-DD') 
           and id='myID' 
           and to_number(to_char(dateCol2, 'hh24mi')) < 1730)
 where first_row = 1
    or last_row = 1;

答案 1 :(得分:1)

如果你想为每个日期(dateCol)选择最近的dateCol2的整行,那么在17:30之前,我会选择像

这样的东西。
   select * 
     from table
    where (dateCol, dateCol2)  in (
        select dateCol, max(dateCol2) 
          from table 
         where dateCol between to_date('2013-01-01','YYYY-MM-DD') 
                           and to_date('2013-01-01','YYYY-MM-DD') 
           and id='myID' 
           and to_char(dateCol2, 'hh24:mi') < '17:30'
         group by dateCol
    )

不确定你的日期条件是否相同,但我认为你刚刚输入了两次相同的日期。

编辑:

       select * 
         from table t
  innner join (
            select dateCol, 
                   min(dateCol2) first, 
                   max(dateCol2) last
              from table 
             where trunc(dateCol) = to_date('2013-01-01','YYYY-MM-DD') 
               and id='myID' 
               and to_char(dateCol2, 'hh24:mi') < '17:30'
             group by dateCol
              ) 
              sub s
           on t.dateCol  = s.dateCol
          and (t.dateCol2 = s.first
            or t.dateCol2 = s.last)

答案 2 :(得分:1)

我认为你可以这样做,即使它不是很聪明...... dateCol

select * from table 
where dateCol between to_date('2013-01-01','YYYY-MM-DD') 
                  and to_date('2013-01-01','YYYY-MMDD') 
and id='myID' and dateCol<trunc(dateCol)+17.5/24

答案 3 :(得分:0)


    select trunc(t.dateCol),
       max(t.your_column) keep (dense_rank last order by t.dateCol)
    from table t 
    where t.dateCol between to_date('2013-01-01','YYYY-MM-DD') and to_date('2013-05-01','YYYY-MM-DD') 
      and t.id='myID' 
      and t.dateCol