Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Max [date] from DIFFERENT columns?
Hi,

I have a table in which there are several date columns recording different
event points occuring to a record (e.g., date opened, date action-1, etc).

I need to find the most recent date (MAX(date-n)) from across all these
columns to compare with a final closure date.

Is there a (simple/sensible) mechanism that makes this possible?

I have considered using IF/ELSE to try and determine if one is later than
the other, but this seems like a no-go (too complex to implement sensibly).

CASE statement instead maybe?

Any pointers gratefully received...Thx

Al




Report this thread to moderator Post Follow-up to this message
Old Post
Alec MacLean
04-06-06 02:23 PM


RE: Max [date] from DIFFERENT columns?
Alec,

If you do not have a lot of columns, you can use the following approach:

Please let me kow if it helps...

-- BEGIN SCRIPT

declare @table table
(RecordID int
, Created datetime
, Opened datetime
, Updated datetime
)

insert into @table
values (1, getdate(), getdate()+ .10, getdate()+.15)
insert into @table
values (2, getdate()+.3, getdate()+ .40, getdate()+.45)

-- Preview of the table
select * from @table

-- Actual query
select RecordID
, MAX(ActionDate) LatestActionDate
from	(
select RecordId
, Created ActionDate
, 'Created' Action
from @table
union
select RecordId
, Opened ActionDate
, 'Opened' Action
from @table
union
select RecordId
, Updated ActionDate
, 'Updated' Action
from @table
) t1
group by RecordID

-- END SCRIPT
"Alec MacLean" wrote:

> Hi,
>
> I have a table in which there are several date columns recording different
> event points occuring to a record (e.g., date opened, date action-1, etc).
>
> I need to find the most recent date (MAX(date-n)) from across all these
> columns to compare with a final closure date.
>
> Is there a (simple/sensible) mechanism that makes this possible?
>
> I have considered using IF/ELSE to try and determine if one is later than
> the other, but this seems like a no-go (too complex to implement sensibly)
.
>
> CASE statement instead maybe?
>
> Any pointers gratefully received...Thx
>
> Al
>
>
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Edgardo Valdez, MCSD, MCDBA
04-07-06 01:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 07:41 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006