January 28, 2010 at 12:59 pm
i need a select query for below.
there is 3 date colums in a table.
how to find out the greatest value from these 3 columns.
January 28, 2010 at 1:03 pm
Sounds like homework.
If not, please provide sample data, expected result and what you've tried so far as described in the first link in my signature.
January 28, 2010 at 1:06 pm
selecting one greatest value from 3 coloumns.?
January 28, 2010 at 1:07 pm
selecting one greatest value from 3 coloumns in a table.?
January 28, 2010 at 1:10 pm
charipg (1/28/2010)
selecting one greatest value from 3 coloumns in a table.?
Yes.
How about sample data including your current query as requested before?
January 28, 2010 at 1:13 pm
Try MAX with an included CASE WHEN.
Greets
Flo
January 28, 2010 at 1:14 pm
I am not able to provide the sample data.
can you pls provide the sample query?
January 28, 2010 at 1:15 pm
charipg (1/28/2010)
I am not able to provide the sample data.can you pls provide the sample query?
?
January 28, 2010 at 1:19 pm
charipg (1/28/2010)
I am not able to provide the sample data.can you pls provide the sample query?
You don't have to provide your real data. Get the basic concept and transform it into a sample table with fake column names and fake values. But make sure it represents what you're struggling with.
Please remember we don't see what you see. So you have to help us help you.
January 28, 2010 at 1:37 pm
Please correct the below query..........
select max(myDate) from
( select field_1 myDate from myTable
union all
select field_2 myDate from myTable
union all
select field_3 myDate from myTable)
January 28, 2010 at 1:46 pm
charipg (1/28/2010)
Please correct the below query..........select max(myDate) from
( select field_1 myDate from myTable
union all
select field_2 myDate from myTable
union all
select field_3 myDate from myTable)
If you add an alias at the end of your query to give your UNION statement a "pseudo table name", the query will return a result.
Note: Since you didn't mention whether you get an error message (or even what that error would have been) I'm just guessing that this is what you're looking for...
select max(myDate) from
( select field_1 myDate from myTable
union all
select field_2 myDate from myTable
union all
select field_3 myDate from myTable) uniontable
January 28, 2010 at 1:48 pm
Your approach should work, doesn't it?
Maybe this could be a better performance:
SELECT
MAX(
CASE WHEN field1 > field2 THEN
CASE WHEN field1 > field3 THEN
field1
ELSE field3 END
ELSE
CASE WHEN field2 > field3 THEN
field2
ELSE field3 END
END
)
FROM myTable
Greets
Flo
January 28, 2010 at 1:50 pm
Didn't notice the missing column name(s)...
January 28, 2010 at 1:51 pm
Looks like someone beat me to the punch.
Aaron Hall
IT Infrastructure Consultant
Nothing is more confounding than a DBA that uses bureaucracy as a means to inflate his power. Ever try to get an index added to a government run SQL server and you'll know what I mean.
March 30, 2010 at 9:35 am
select Max(date)
from
(select date1 from table1
union all
select date2 from table1
union all
select date3 from table1 ) as tbl
Viewing 15 posts - 1 through 15 (of 33 total)
You must be logged in to reply to this topic. Login to reply