Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Max values across multiple columns

Status
Not open for further replies.

dhulbert

Technical User
Jun 26, 2003
1,136
GB
Is there an easy way to select the maximim value across muliple columns

I have 5 columns each with a datetime in them and I need to find the maxumim datetime from the 5 colums.

Thanks.

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 

In a single row.

So

uniqueid, date1, date2, date3, date4,......

And I'm after uniqueid, Max(date)

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 
Code:
SELECT MAX(adate) AS max_date
  FROM ( SELECT date1 AS adate
           FROM daTable
          WHERE uniqueid = 937
         UNION ALL
         SELECT date2
           FROM daTable
          WHERE uniqueid = 937
         UNION ALL
         SELECT date3
           FROM daTable
          WHERE uniqueid = 937
         UNION ALL
         SELECT date4
           FROM daTable
          WHERE uniqueid = 937
         UNION ALL
         SELECT date5
           FROM daTable
          WHERE uniqueid = 937
       ) AS d
:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top