选择按日期分组最高分,充分显示日期时间
-
12-09-2019 - |
题
g·天,我有一个表,其中显示了一系列的分数和日期时间的分数的那些发生。 我想选择最大这些分数的每一天的,但显示该得分发生日期时间。
我使用Oracle数据库(10克)和表的结构,如下所示:
scoredatetime score (integer) --------------------------------------- 01-jan-09 00:10:00 10 01-jan-09 01:00:00 11 01-jan-09 04:00:01 9 ...
我想能够呈现的结果,例如以上变为:
01-jan-09 01:00:00 11
这下面的查询让我中途有..但不是所有的方式。
select
trunc(t.scoredatetime), max(t.score)
from
mytable t
group by
trunc(t.scoredatetime)
我不能得分加入只是因为相同的高分可能已在一天实现多次。
我感谢您的帮助!
西蒙爱德华兹
解决方案
with mytableRanked(d,scoredatetime,score,rk) as (
select
scoredatetime,
score,
row_number() over (
partition by trunc(scoredatetime)
order by score desc, scoredatetime desc
)
from mytable
)
select
scoredatetime,
score
from mytableRanked
where rk = 1
order by date desc
在一天内的多个高分的情况下,这将返回对应于该发生在日最新的一个的行。如果你想查看某一天的所有得分最高,在ROW_NUMBER窗口中删除从订单scoredatetime递减的规范。
另外,你可以做到这一点(它会列出一个日期高分的关系):
select
scoredatetime,
score
from mytable
where not exists (
select *
from mytable as M2
where trunc(M2.scoredatetime) = trunc(mytable.scoredatetime)
and M2.score > mytable.scoredatetime
)
order by scoredatetime desc
其他提示
首先,你还不指定是否在同一天中的两个或多个行包含相等的高分应该发生什么。
两个可能的答案对这个问题:
1)只要选择scoredatetime的之一,它并不重要的一个
在这种情况下,不要使用自加入或分析,你在其他的答案看,因为有一个特殊的聚合功能,可以做你的工作更有效率。一个示例:
SQL> create table mytable (scoredatetime,score)
2 as
3 select to_date('01-jan-2009 00:10:00','dd-mon-yyyy hh24:mi:ss'), 10 from dual union all
4 select to_date('01-jan-2009 01:00:00','dd-mon-yyyy hh24:mi:ss'), 11 from dual union all
5 select to_date('01-jan-2009 04:00:00','dd-mon-yyyy hh24:mi:ss'), 9 from dual union all
6 select to_date('02-jan-2009 00:10:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual union all
7 select to_date('02-jan-2009 01:00:00','dd-mon-yyyy hh24:mi:ss'), 1 from dual union all
8 select to_date('02-jan-2009 04:00:00','dd-mon-yyyy hh24:mi:ss'), 0 from dual
9 /
Table created.
SQL> select max(scoredatetime) keep (dense_rank last order by score) scoredatetime
2 , max(score)
3 from mytable
4 group by trunc(scoredatetime,'dd')
5 /
SCOREDATETIME MAX(SCORE)
------------------- ----------
01-01-2009 01:00:00 11
02-01-2009 01:00:00 1
2 rows selected.
2)与最大得分选择的所有记录。
在这种情况下,需要用一个RANK或DENSE_RANK功能分析。一个示例:
SQL> select scoredatetime
2 , score
3 from ( select scoredatetime
4 , score
5 , rank() over (partition by trunc(scoredatetime,'dd') order by score desc) rnk
6 from mytable
7 )
8 where rnk = 1
9 /
SCOREDATETIME SCORE
------------------- ----------
01-01-2009 01:00:00 11
02-01-2009 00:10:00 1
02-01-2009 01:00:00 1
3 rows selected.
此致 罗布。
您可能需要两个SELECT语句来退出这个功能:第一,收集截断日期和相关的最高分,第二个与分数相关的实际datetime值拉
。尝试:
SELECT T.ScoreDateTime, T.Score
FROM
(
SELECT
TRUNC(T.ScoreDateTime) ScoreDate, MAX(T.score) BestScore
FROM
MyTable T
GROUP BY
TRUNC(T.ScoreDateTime)
) ByDate
INNER JOIN MyTable T
ON TRUNC(T.ScoreDateTime) = ByDate.ScoreDate and T.Score = ByDate.BestScore
ORDER BY T.ScoreDateTime DESC
这将拉动在最好的得分关系和
有关该每一天仅选择最近张贴高分一个版本:
SELECT T.ScoreDateTime, T.Score
FROM
(
SELECT
TRUNC(T.ScoreDateTime) ScoreDate,
MAX(T.score) BestScore,
MAX(T.ScoreDateTime) BestScoreTime
FROM
MyTable T
GROUP BY
TRUNC(T.ScoreDateTime)
) ByDate
INNER JOIN MyTable T
ON T.ScoreDateTime = ByDate.BestScoreTime and T.Score = ByDate.BestScore
ORDER BY T.ScoreDateTime DESC
这可以产生每个日期的多条记录,如果两个不同的分数在完全相同的时间发布。
不隶属于 StackOverflow