Results 1 to 2 of 2
  1. #1
    Robeen is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596

    Returning highest Rank/Row_Number over Partition.

    Hi,

    I have this working SQL Server query:
    Code:
    SELECT Top 100 XNUM, SerialNo, Amount, Rank() Over(Partition By XNum Order By Serialno Desc) As MostRecent 
    From TableName 



    What I'd like to do is add a where clause & have the query return only rows which have a Rank (MostRecent) of 1.

    I tried adding "Where MostRecent = 1" but that doesn't work.

    I have put the above SQL in a CTE and made it work but I would rather find a way of doing it as it would make it easier for me to use it as part of a bigger query.
    I have also been able to make it work like this:
    Code:
    Select * From
    (
    SELECT Top 100 XNUM, SerialNo, Amount, Rank() Over(Partition By XNum Order By Serialno Desc) As MostRecent 
    From TableName 
    )
    Where MostRecent = 1
    Thanks!!

    Robeen


















  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Review http://allenbrowne.com/ranking.html

    Note what Allen says about limitations of ranking in query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Returning only the highest value
    By woo8118 in forum Queries
    Replies: 5
    Last Post: 05-22-2015, 08:03 AM
  2. Replies: 2
    Last Post: 07-02-2014, 05:59 PM
  3. Partition with Access SQL
    By b82726272 in forum Queries
    Replies: 1
    Last Post: 04-08-2014, 03:59 AM
  4. Replicating ROW_NUMBER OVER (PARTITION BY...)
    By normanj in forum Queries
    Replies: 2
    Last Post: 11-27-2012, 07:36 AM
  5. please need help RANK
    By Andyjones in forum Queries
    Replies: 8
    Last Post: 09-10-2011, 12:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums