Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Using Max Function

Author  Topic 

JezLisle
Posting Yak Master

132 Posts

Posted - 2009-10-19 : 09:57:50
How is it possible to find the Max date across a number of fields?

I have

Appt1
Appt2
Appt3
Appt4
Appt5

How can I find the max value for each record over these fields?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-19 : 10:03:23
[code]
select max(appt)
from
(
select max(Appt1) as appt from sometable union all
select max(Appt2) as appt from sometable union all
select max(Appt3) as appt from sometable union all
select max(Appt4) as appt from sometable union all
select max(Appt5) as appt from sometable
) a
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

JezLisle
Posting Yak Master

132 Posts

Posted - 2009-10-19 : 10:09:50
Thanks, but will that be for the whole table or will it work for each record? as I want to find this for each record in the table
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-19 : 10:12:41
that for whole table. For each record, you write a UDF to do that

and in the UDF,


select max(appt)
from
(
select @appt1 as appt union all
select @appt2 as appt union all
select @appt3 as appt union all
select @appt4 as appt union all
select @appt5 as appt
) a



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

JezLisle
Posting Yak Master

132 Posts

Posted - 2009-10-19 : 10:15:29
how do you mean UDF?

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-19 : 10:16:31
UDF = User Defined Function


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -