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 Server – "Denali" – OVER ( … ROWS / RANGE … )

Aug 16 2011 5:17PM by Vishal Gajjar   

ROWS/RANGE allows you to change the frame size within a partition. Valid arguments for ROWS are:

UNBOUNDED PRECEDING Starts the window at first row of the partition
UNBOUNDED FOLLOWING Ends the window at last row of the partition
CURRENT ROW Starts or Ends the window at current row
N PRECEDING Starts the window N rows before current row
N FOLLOWING Ends the window N rows after current row

RANGE is only supported with UNBOUNDED and CURRENT ROW frame delimiters, It cannot be used with N PRECEDING or N FOLLOWING.

ROWS UNBOUNDED PRECEDING:

A simple example to demonstrate use of ROWS is generating cumulative total. Consider following data:

Year        Month       Amount
2011        1           1000.00
2011        2           2000.00
2011        3           3000.00
2011        4           4000.00
2011        5           5000.00
2011        6           6000.00
2011        7           7000.00

To generate a cumulative total we need sum of all rows till current row. This can be done by using UNBOUNDED PRECEDING:

SELECT [Year], [Month],
       SUM([Amount]) OVER (ORDER BY [Year], [Month] ROWS UNBOUNDED PRECEDING)
       AS [Amount]
FROM   dbo.SalesData
GROUP BY [Year], [Month], [Amount]

When no starting or ending position is defined, it defaults to CURRENT ROW. I.e. for Month = 3, Amount is generated as sum of Amount of Month = 1 + Amount of Month = 2 + Amount of Month = 3

Result Set:

Year        Month       Amount
2011        1          1000.00
2011        2          3000.00
2011        3          6000.00
2011        4          10000.00
2011        5           15000.00
2011        6           21000.00
2011        7           28000.00

ROWS BETWEEN N PRECEDING AND CURRENT ROW:

By using N PRECEDING you can specify to start frame N rows before current row. For example, to generate Amount by current month + previous month:

SELECT [Year], [Month],
       SUM([Amount]) OVER (ORDER BY [Year], [Month] ROWS 1 PRECEDING)
       –End default to CURRENT ROW
       –Or you can also write
       –SUM([Amount])
       –OVER (ORDER BY [Year], [Month] ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
       –To make is more readable
       AS [Amount]
FROM   dbo.SalesData
GROUP BY [Year], [Month], [Amount]

Result set:

Year        Month       Amount
2011        1          1000.00
2011        2           3000.00
2011        3           5000.00
2011        4           7000.00
2011        5           9000.00
2011        6          11000.00
2011        7           13000.00

More combinations to try:

CREATE TABLE dbo.SalesData
(     
       [Year]        INT,
       [Month]       INT,
       [Amount]      MONEY
)
 
INSERT INTO dbo.SalesData VALUES
(2011, 1, 1000.00), (2011, 2, 2000.00), (2011, 3, 3000.00),
(2011, 4, 4000.00), (2011, 5, 5000.00), (2011, 6, 6000.00),
(2011, 7, 7000.00)

SELECT [Year], [Month], [Amount],
       –SUM([Amount]) OVER (ORDER BY [Year], [Month] ROWS 1 PRECEDING)
       –SUM([Amount]) OVER (ORDER BY [Year], [Month] ROWS 1 FOLLOWING)
       –SUM([Amount]) OVER (ORDER BY [Year], [Month]
       –     ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
       –SUM([Amount]) OVER (ORDER BY [Year], [Month]
       –     RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
       –SUM([Amount]) OVER (ORDER BY [Year], [Month] RANGE UNBOUNDED PRECEDING)
              AS [SUM]
FROM   dbo.SalesData
GROUP BY [Year], [Month], [Amount]

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]