我正在寻找为 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
)