超过乘法列的最大值 [英] Max over multiply columns

查看:72
本文介绍了超过乘法列的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:

在我的表中,这些行如下:

In my table I have these rows:

PersHist :

ID    Date         Histroy
1     01.01.2008   0
1     01.01.2008   1
1     01.01.2008   2
1     02.01.2008   0
1     02.01.2008   1

现在,当我进行选择时:

Now when I do a select like:

SELECT max(date), max(Histroy) 
FROM PersHist
WHERE ID = 1

我得到以下输出:

ID    Date         Histroy
1     02.01.2008   2

这是错误的,因为没有02.01.2008/2(日期/历史记录)

This is false because there is no 02.01.2008/2 (Date/Histroy)

是否可以编写一个简单的SQL来获取我02.01.2008/1 没有编写子查询?

Is it possible to write a simple SQL that will get me 02.01.2008/1 without writing subqueries?

推荐答案

尝试一下.它将接受所有ID = 1的条目,按日期(从最新到最旧)排序,然后按历史记录(降序排列),然后返回给您第一个条目(即该日期中最新的日期和历史记录最高的历史记录).

Try this. It will take all entries with ID=1, order them by Date (latest to oldest) and then by Histroy (descending) and then return to you the first entry (i.e. with latest Date and highest Histroy within this Date).

SELECT * FROM 
(SELECT ID, Date, Histroy
 FROM PersHist
 WHERE ID = 1
 ORDER BY Date DESC, Histroy DESC)
WHERE ROWNUM = 1

不幸的是,如果没有子查询,它将无法正常工作. Oracle首先将ROWNUM归因于ORDER s

Unfortunately, it does not work without a subquery. Oracle first attributes the ROWNUM and then ORDERs

这篇关于超过乘法列的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆