in

Dé specialist in .NET trainingen en consultancy

Erik van Appeldoorn

juni 2009 - Posts

  • Ranking the stars

    While preparing a SQL Server 2008 Transact-SQL course. The ranking capabilities of SQL Server took my attention. From SQL Server 2005 on these powerful functions exist and can be very helpful. For a small demo I created a table with twelve stars and their distance in light-years from the earth.

    1. ROW_NUMBER()

    To create an additional column in your result set with a rowid based on the Order By clause there is the ROW_NUMBER() function



    SELECT ROW_NUMBER() OVER (ORDER BY s.Distance) as rowid,
           s.Name,
           s.Distance
    FROM Star s  
     
    rowid Name Distance
    1 Proxima Centauri 4
    2 Alpha Centauri A 4
    3 Alpha Centauri B 4
    4 Ster van Barnard 6
    5 Wolf 359 8
    6 Lalande 21185 8
    7 Sirius A 9
    8 Sirius B 9
    9 UV Ceti 9
    10 BL Ceti 9
    11 Ross 154 10
    12 Ross 248 10
     

    2. RANK()

    To change the rowid to a ranking just change the ROW_NUMBER() function to RANK()

    SELECT RANK() OVER (ORDER BY s.Distance) as ranking,
           s.Name,
           s.Distance
    FROM Star s  
      
    ranking Name Distance
    1 Proxima Centauri 4
    1 Alpha Centauri A 4
    1 Alpha Centauri B 4
    4 Ster van Barnard 6
    5 Wolf 359 8
    5 Lalande 21185 8
    7 Sirius A 9
    7 Sirius B 9
    7 UV Ceti 9
    7 BL Ceti 9
    11 Ross 154 10
    11 Ross 248 10

     

    3. DENSE_RANK()

    RANK() skips id’s when records have duplicate values. In most situations this is just the behaviour you need. If not try the DENSE_RANK() function.

    SELECT DENSE_RANK() OVER (ORDER BY s.Distance) as ranking,
           s.Name,
           s.Distance
    FROM Star s   
     
    ranking Name Distance
    1 Proxima Centauri 4
    1 Alpha Centauri A 4
    1 Alpha Centauri B 4
    2 Ster van Barnard 6
    3 Wolf 359 8
    3 Lalande 21185 8
    4 Sirius A 9
    4 Sirius B 9
    4 UV Ceti 9
    4 BL Ceti 9
    5 Ross 154 10
    5 Ross 248 10

     

    4. NTILE(x)

    Another possibility is to divide the results in groups. So let’s say we what to categorise the stars in four groups: very close, close, far and very far. This can be achieved by:

    SELECT NTILE(4) OVER (ORDER BY s.Distance) as groups,
           s.Name,
           s.Distance
    FROM Star s   
     
    groups Name Distance
    1 Proxima Centauri 4
    1 Alpha Centauri A 4
    1 Alpha Centauri B 4
    2 Ster van Barnard 6
    2 Wolf 359 8
    2 Lalande 21185 8
    3 Sirius A 9
    3 Sirius B 9
    3 UV Ceti 9
    4 BL Ceti 9
    4 Ross 154 10
    4 Ross 248 10

     

    By adding a CASE statement you can easily convert the group number into a description:

     

    SELECT groups = CASE NTILE(4) OVER (ORDER BY s.Distance)
           WHEN 1 THEN 'very close'
           WHEN 2 THEN 'close'
           WHEN 3 THEN 'far'
           WHEN 4 THEN 'very far'
           END,
           s.Name,
           s.Distance
    FROM Star s  
     
    groups Name Distance
    very close Proxima Centauri 4
    very close Alpha Centauri A 4
    very close Alpha Centauri B 4
    close Ster van Barnard 6
    close Wolf 359 8
    close Lalande 21185 8
    far Sirius A 9
    far Sirius B 9
    far UV Ceti 9
    very far BL Ceti 9
    very far Ross 154 10
    very far Ross 248 10

    have fun!

  • Parallel sessions a deadly dilemma

    Everyone who frequently visits conferences knows about this phenomenon. On the first day at the DevDays 09 there was a great session in the beginning of the afternoon from Mike Taulty named: What’s new in Silverlight 3. At the same time Sara Ford did her session about Visual Studio IDE Tips.

    I have been to several sessions of Mike in the past and he is one of my favourite speakers. You will never get in a PowerPoint coma at his sessions and he goes flat out all the way. Next to this I was curious about all the new features in the new Silverlight version. On the other hand I have never been to a Sara Ford session but I am a great fan of her IDE Tip of the day blog. How would her session be? Is she a good speaker?

    Well I don’t know because I decided to attend Mike’s session.  If you did as well, please be sure to check her 24 Visual Studio IDE Tips here:

    http://blogs.msdn.com/saraford/archive/2009/05/28/devdays-09-24-visual-studio-tips.aspx

    Sorry Sara, the next opportunity I will be there. I swear.