Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My Experiments with SQLServer
Browse by Tags · View All
SQLServer 126
SQLServer 2008 R2 91
SQLServer 2008 86
SQLServer 2005 82
SQL 60
Database 59
Sql And Me 57
Tips & Tricks 28
SQL Server 27
SQL FAQ 26

Archive · View All
May 2011 25
June 2011 21
July 2011 21
August 2011 19
April 2011 16
January 2013 4
May 2012 3
April 2012 3
October 2011 3
February 2013 2

Vishal Gajjar's Blog

SQL Functions – ROW_NUMBER()

Jul 1 2011 8:16PM by Vishal Gajjar   

ROW_NUMBER() can be used to generate a sequential number for each row in the result set. Unlike RANK() and DENSE_RANK(), ROW_NUMBER() in case of ties it does not generate same number, it simply ignores the tie and generates sequential numbers for each of the tied rows. For example,

-- © 2011 – Vishal (http://SqlAndMe.com)
 
SELECT      Products.ProductID, Products.Name,
            Products.Category, Products.Price,
            ROW_NUMBER() OVER (ORDER BY [Price])
            AS [RowNumber]
FROM
(
      SELECT 804 AS [ProductID],
      'HL Fork' AS [Name], 'Accessories' AS [Category],
      229.49 AS [Price] UNION ALL
      SELECT 871, 'Mountain Bottle Cage', 'Accessories', 9.99    UNION ALL
      SELECT 921, 'Mountain Tire Tube',   'Parts',       24.99   UNION ALL
      SELECT 845, 'Mountain Pump',        'Parts',       24.99   UNION ALL
      SELECT 980, 'Mountain-400-W Silver','Accessories', 769.49  UNION ALL
      SELECT 988, 'Mountain-500 Silver',  'Parts',       564.99  UNION ALL
      SELECT 793, 'Road-250 Black',       'Accessories', 2443.35 UNION ALL
      SELECT 762, 'Road-650 Red',         'Parts',       782.99  UNION ALL
      SELECT 923, 'Touring Tire Tube',    'Accessories', 4.99    UNION ALL
      SELECT 966, 'Touring-1000 Blue',    'Parts',       2384.07
) Products

ROW_NUMBER() Result Set:

ProductID   Name                  Category    Price         RowNumber
———–        ———————               ———–        ————-         ————
923         Touring Tire Tube     Accessories 4.99          1
871         Mountain Bottle Cage  Accessories 9.99          2
845         Mountain Pump         Parts       24.99         3
921         Mountain Tire Tube    Parts       24.99         4
804         HL Fork               Accessories 229.49        5
988         Mountain-500 Silver   Parts       564.99        6
980         Mountain-400-W Silver Accessories 769.49        7
762         Road-650 Red          Parts       782.99        8
966         Touring-1000 Blue     Parts       2384.07       9
793         Road-250 Black        Accessories 2443.35       10
 
(10 row(s) affected)

If we had used RANK() function, it would have generated the result as below:

……………
871         Mountain Bottle Cage  Accessories 9.99            2
845         Mountain Pump         Parts       24.99           3
921         Mountain Tire Tube    Parts       24.99           3
804         HL Fork               Accessories 229.49          5
……………

and DENSE_RANK() would have generated 4 for 'HL Fork'.

Using ROW_NUMBER with <PARTITION BY>:

By using PARTITION BY clause with ROW_NUMBER() the result set can be divided into number of result sets based on the PARTITION BY column, and to each result set ROW_NUMBER() function will be applied independently.

We can apply PARTITION BY clause to above data to partition the result sets by Product Category.

The result set would contain:

 Partition By Category
10 Rows ->  Accessories (5 Rows) -> Apply ROW_NUMBER() -> 1, 2, 3, 4, 5
            Parts (5 Rows)       -> Apply ROW_NUMBER() -> 1, 2, 3, 4, 5

 

SELECT      Products.ProductID, Products.Name,
            Products.Category, Products.Price,
            ROW_NUMBER() OVER (PARTITION BY [Category] ORDER BY [Price])
            AS [RowNumber]
FROM
(
      SELECT 804 AS [ProductID],
      'HL Fork' AS [Name], 'Accessories' AS [Category],
      229.49 AS [Price] UNION ALL
      SELECT 871, 'Mountain Bottle Cage', 'Accessories', 9.99    UNION ALL
      SELECT 921, 'Mountain Tire Tube',   'Parts',       24.99   UNION ALL
      SELECT 845, 'Mountain Pump',        'Parts',       24.99   UNION ALL
      SELECT 980, 'Mountain-400-W Silver','Accessories', 769.49  UNION ALL
      SELECT 988, 'Mountain-500 Silver',  'Parts',       564.99  UNION ALL
      SELECT 793, 'Road-250 Black',       'Accessories', 2443.35 UNION ALL
      SELECT 762, 'Road-650 Red',         'Parts',       782.99  UNION ALL
      SELECT 923, 'Touring Tire Tube',    'Accessories', 4.99    UNION ALL
      SELECT 966, 'Touring-1000 Blue',    'Parts',       2384.07
) Products

Result Set:

ProductID   Name                  Category    Price         RowNumber
———–        ———————               ———–        ———–          ————
923         Touring Tire Tube     Accessories 4.99          1
871         Mountain Bottle Cage  Accessories 9.99          2
804         HL Fork               Accessories 229.49        3
980         Mountain-400-W Silver Accessories 769.49        4
793         Road-250 Black        Accessories 2443.35       5
845         Mountain Pump         Parts       24.99         1
921         Mountain Tire Tube    Parts       24.99         2
988         Mountain-500 Silver   Parts       564.99        3
762         Road-650 Red          Parts       782.99        4
966         Touring-1000 Blue     Parts       2384.07       5
 
(10 row(s) affected)

Hope This Helps! Cheers!


Republished from Sql&Me [31 clicks].  Read the original version here [32134 clicks].

Vishal Gajjar
46 · 4% · 1276
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]