Select Max problem

  • Hi,

    I´m using SQL 2005 and what is wrong with the query below?

    SELECT MAX( RowNumber ) FROM

    (

    SELECT

    ROW_NUMBER() OVER (ORDER BY MSG_DTM_LASTSTATUSTIME DESC) as RowNumber

    FROM

    Message_MSG msg with(nolock)

    )

    Thanks in advance,

    Fernando

  • try this

    SELECT MAX( RowNumber ) FROM

    (

    SELECT

    ROW_NUMBER() OVER (ORDER BY MSG_DTM_LASTSTATUSTIME DESC) as RowNumber

    FROM

    Message_MSG msg with(nolock)

    ) A

  • Thank you very much!

  • Just add an alias:

    SELECT MAX( RowNumber ) FROM

    (

    SELECT

    ROW_NUMBER() OVER (ORDER BY MSG_DTM_LASTSTATUSTIME DESC) as RowNumber

    FROM

    Message_MSG msg with(nolock)

    ) AS x

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply