• J Livingston SQL (6/25/2013)


    Alan.B (6/25/2013)


    Michael Valentine Jones (6/25/2013)


    select top 1

    a.[Date]

    from

    (

    select top 1 b.[Date] from table1 b order by b.[Date] desc

    union

    select top 1 c.[Date] from table2 c order by c.[Date] desc

    ) a

    order by

    a.[Date] desc

    I think he has two tables and is trying to get the max date for each row. If he was trying to get the max date for both columns (a single value as your query would return) then this would be cleaner and faster:

    SELECT MAX([date]) [Date] FROM

    (SELECT [date] FROM table1

    UNION

    SELECT [date] FROM table2) AS a

    as per OP request

    Hi ,

    The colums are present in 2 separate tables with datatype Date. I want the date which would be max of both.

    Table1 (col1 Date)

    Table2 (col2 Date)

    Table1('2013-06-25') and Table2('2013-06-24')

    So the answer would be 2013-06-25

    Which is why I included two solutions: My original solution which I believe is correct (Shadab, please feel free to chime in 😉 ) and one which was cleaner and faster than the one Michael posted.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001