date value
18/5/2010, 1 pm 40
18/5/2010, 2 pm 20
18/5/2010, 3 pm 60
18/5/2010, 4 pm 30
18/5/2010, 5 pm 60
18/5/2010, 6 pm 25
i need to query for the row having max(value)(i.e. 60). So, here we get two rows. From that, I need the row with the lowest time stamp for that day(i.e 18/5/2010, 3 pm -> 60)
Keywords like TOP, LIMIT, ROWNUM, ...etc are database dependent. Please read this article for more information.
http://en.wikipedia.org/wiki/Select_(SQL)#Result_limits
Oracle: ROWNUM could be used.
Answering the question more specifically:
In Oracle DB:
Technically, this is the same answer as @Sujee. It also depends on your version of Oracle as to whether it works. (I think this syntax was introduced in Oracle 12??)
As I say, if you look under the bonnet, I think this code is unpacked internally by the Oracle Optimizer to read like @Sujee's. However, I'm a sucker for pretty coding, and nesting
select
statements without a good reason does not qualify as beautiful!! :-PThe simplest answer would be
--Setup a test table called "t1"
-- Load the data. -- Note: date format different than in the question
-- find the row with the max qty and min date.
I know you can do the "TOP 1" answer, but usually your solution gets just complicated enough that you can't use that for some reason.
Regards, Rob.
Answer is to add a having clause:
this should work and gets rid of the neccesity of having an extra sub select in the date clause.