O'Reilly logo

Microsoft SQL Server 2012 T-SQL by Debra Aaron, Tom Coffing

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10 – Window Functions OLAP

“Don’t count the days, make the days count.”

- Mohammad Ali

The Row_Number Command

SELECT Product_ID ,Sale_Date , Daily_Sales,

               ROW_NUMBER() OVER

                  (ORDER BY Product_ID, Sale_Date) AS Seq_Number

FROM    Sales_Table  WHERE Product_ID IN (1000, 2000) ;

image

The ROW_NUMBER() Keyword(s) caused Seq_Number to increase sequentially. Notice that this does NOT have a Rows Unbounded Preceding, and it still works!

Quiz – How did the Row_Number Reset?

SELECT Product_ID ,Sale_Date , Daily_Sales,

 ROW_NUMBER() OVER (PARTITION BY Product_ID

      ORDER BY Product_ID, Sale_Date )   AS StartOver

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required