如何选择“最大"从表排? [英] How can I select the "maximum" row from a table?

查看:137
本文介绍了如何选择“最大"从表排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从表中选择最大的行?最大值是什么意思-我的表有两个时间戳列,TIME1和TIME2.最大列是具有TIME1的最新值的列.如果那不是唯一的行,则最大值为那些行中具有TIME2最新值的那一行.

How can I select the maximum row from a table? What does maximum mean -- well my table has two timestamp columns, TIME1 and TIME2. The maximum column is the one with the latest value for TIME1. If that is not a unique row, then the maximum is the one within those rows with the latest value for TIME2.

如果重要的话,这是在Oracle上的.

This is on Oracle if that matters.

推荐答案

您需要的是"Top-N"查询:

What you need is a "Top-N" query:

从( 从表中选择* 按time1 desc,time2 desc排序 ),其中rownum< 2;

select * from ( select * from table order by time1 desc, time2 desc ) where rownum < 2;

如果您在time1和time2上正确建立索引,那将非常快:

if you properly index on time1, time2 it will be very fast:

http://blog.fatalmind.com/2010/07/30/analytic-top-n-queries/

这篇关于如何选择“最大"从表排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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