I want to write a SQL that returns multiple columns with a select max on one of them.
Let me clearify with an example.
Practically I’d like to have something like this:
Select max(from_date)
,func_status_code
,name
from table
where from_date <= current date
So what I want is to perform a select max on a column but also get the other columns for that row/those rows.
from_date func_status_code name
2012-08-21 1 A
2012-08-21 4 A
2012-08-20 5 A
2012-08-20 3 A
returning
from_date func_status_code name
2012-08-21 1 A
2012-08-21 4 A
I know I could do a subselect, something like:
select from_date
,func_status_code
,name
from table
where from_date = (Select max(from_date)
from table
where from_date <= current date
)
But I can’t understand why the other method doesn’t work.
Does anybody know how to achieve this?
One way you can do this is with window/analytic functions: