Rankings in T-SQL

  •  Here is a code snippet from The Guru's Guide to Transact-SQL (p. 126):

    SELECT s.State,

     st.Stor_Name,

     s.TotalSales,

     Rank = COUNT(*)

    FROM SalesByState s  -- The VIEW created above

    JOIN  SalesByState t

    ON (s.State = t.State)

    JOIN Stores st

    ON (s.Stor_ID = st.Stor_ID)

    WHERE s.TotalSales <= t.TotalSales

    GROUP BY s.State,

      st.Stor_Name,

      s.TotalSales

    HAVING COUNT(*) <= 1

    ORDER BY s.State, Rank

    ====================

    I may just be dense, but for the life of me I can't figure out how this code calculates the rankings.  Would someone be kind enough to step me through what's happening under the covers here so that I come to some understanding of how these rankings are achieved?

    I've tried generating some other queries to get a better picture of how the Rankings are built along the way, but I keep getting lost.

    Your help is appreciated.  Thanks in advance.


    Richard D. Cushing
    Sr. Consultant
    SYNGERGISTIC SOFTWARE SOLUTIONS, LLC

  • Does this help?

    http://support.microsoft.com/?id=186133



    --Jonathan

  • Perfect!  Thanks for the reference.

     


    Richard D. Cushing
    Sr. Consultant
    SYNGERGISTIC SOFTWARE SOLUTIONS, LLC

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

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