我的表格中包含带有时间戳的列日期,以秒为单位的值,并且具有类似
的查询表
2017-01-10 06:45:00 PM 1119
2017-01-10 03:30:00 PM 1054
2017-01-11 11:15:00 PM 379
2017-01-10 06:30:00 PM 377
2017-01-11 09:15:00 PM 375
查询
SELECT
TO_char(DtTm,'YYYY-MM-DD hh:mi:ss AM') As DataDt,
max(MaxSec) as Wait_sec, DtTimeTable.HrID,
FROM DtTimeTable
WHERE DtTimeTable.HrName in ('Dept1', 'Dept2', 'Dept3')
AND DtTm BETWEEN to_date('2017-01-08 00:00:00', 'YYYY-MM-DD hh24:mi:ss')
AND to_date('2017-01-10 23:59:59', 'YYYY-MM-DD hh24:mi:ss')
Group by TO_char(DtTm,'YYYY-MM-DD hh:mi:ss AM'),DtTimeTable.HrID
order by Wait_sec desc
这给了我所有记录,如果我添加
select * from
Query1 --(above)
where rownum <1 order by Wait_sec desc, Datadt desc;
我只获得结果集的最高值
如何为每个日期获取DatewithTime,Maxvalue,如
2017-01-10 06:45:00 PM 1119
2017-01-11 11:15:00 PM 379
答案 0 :(得分:2)
尝试这样的事情:
Select *
From (
Select t.*,
Row_number() over(partition by trunc(datecol) order by value desc nulls last) rn
From yourtable t
) where rn = 1;
它根据您的值列的降序分配日期中的行号,然后过滤以获取第一行
答案 1 :(得分:2)
以下解决方案使用分组,MAX()
聚合函数和FIRST/LAST
函数(KEEP DENSE_RANK
)。如果对于某个日期,同一个最高值不止一次达到,则会在当天第一次选择达到该值。
with
test_data( dt, val ) as (
select to_date('2017-01-10 06:45:00 PM', 'yyyy-mm-dd hh:mi:ss AM'), 1119 from dual
union all
select to_date('2017-01-10 03:30:00 PM', 'yyyy-mm-dd hh:mi:ss AM'), 1054 from dual
union all
select to_date('2017-01-11 11:15:00 PM', 'yyyy-mm-dd hh:mi:ss AM'), 379 from dual
union all
select to_date('2017-01-10 06:30:00 PM', 'yyyy-mm-dd hh:mi:ss AM'), 377 from dual
union all
select to_date('2017-01-11 09:15:00 PM', 'yyyy-mm-dd hh:mi:ss AM'), 375 from dual
)
-- end of test data; SQL query begins below this line (use actual table and column names)
select min(dt) keep(dense_rank last order by val) as dt, max(val) as val
from test_data
group by trunc(dt)
order by dt -- if needed
;
DT VAL
---------------------- ----------
2017-01-10 06:45:00 PM 1119
2017-01-11 11:15:00 PM 379
2 rows selected.