web viewbased on several blog post about all the windows functions

4
/* Written by Clint Huijbers @ 2015-01-31 This code demonstrates all the window functions (OVER-clause) you'll be able to use with SQL Server 2012 (and up). Keep updated by following my blog post on: https://clinthuijbers.wordpress.com/2015/03/25/sql-server-window-functions-cheat-sheet-over-clause/ Based on several blog post about all the windows functions (aggregate, and analytic): https://www.simple-talk.com/sql/learn-sql-server/window-functions-in-sql-server-part-2-the-frame/ https://www.simple-talk.com/sql/t-sql-programming/sql-server-2012-window-function-basics/ For more info about Ranking functions (e.g. ROW_NUMBER, RANK, DENSE_RANK, NTILE), please visit: https://www.simple-talk.com/sql/t-sql-programming/sql-server-2012-window-function-basics/ */ DECLARE @StartDate DATE = '2015-01-01' ;WITH GenerateValues AS ( SELECT DATEADD(DAY,Number,@StartDate) AS DateValue ,Number+1 AS Number FROM master..spt_values WHERE number BETWEEN 0 AND 5 AND type = 'P' ), ListOfCategories AS ( SELECT Category,Multiplier FROM (VALUES ('Category1',1),('Category2',2),('Category3',3)) AS s (Category,Multiplier) ) SELECT Category ,DateValue ,'||' AS '||' ,Number*Multiplier AS Value ,SUM(Number*Multiplier) OVER ( PARTITION BY Category ORDER BY DateValue ROWS BETWEEN 3 PRECEDING AND CURRENT ROW ) AS CumSumPrev3ValuesInclCurrent --Cumulative SUM of the previous 3 values, including the current value ,'||' AS '||' ,Number*Multiplier AS Value ,SUM(Number*Multiplier) OVER ( PARTITION BY Category ORDER BY DateValue ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING ) AS CumSumPrev3ValuesExclCurrent --Cumulative SUM of the previous 3 values, excluding the current value ,'||' AS '||' ,Number*Multiplier AS Value ,SUM(Number*Multiplier) OVER ( PARTITION BY Category ORDER BY DateValue ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS CumSumPrevValuesInclCurrent --Cumulative SUM of all previous values, including the current value ,Number*Multiplier AS Value ,SUM(Number*Multiplier) OVER ( PARTITION BY Category

Upload: trinhque

Post on 06-Mar-2018

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Web viewBased on several blog post about all the windows functions

/*Written by Clint Huijbers @ 2015-01-31This code demonstrates all the window functions (OVER-clause) you'll be able to use with SQL Server 2012

(and up).

Keep updated by following my blog post on:https://clinthuijbers.wordpress.com/2015/03/25/sql-server-window-functions-cheat-sheet-over-clause/

Based on several blog post about all the windows functions (aggregate, and analytic):https://www.simple-talk.com/sql/learn-sql-server/window-functions-in-sql-server-part-2-the-frame/https://www.simple-talk.com/sql/t-sql-programming/sql-server-2012-window-function-basics/

For more info about Ranking functions (e.g. ROW_NUMBER, RANK, DENSE_RANK, NTILE), please visit:https://www.simple-talk.com/sql/t-sql-programming/sql-server-2012-window-function-basics/

*/

DECLARE @StartDate DATE = '2015-01-01'

;WITH GenerateValuesAS(

SELECT DATEADD(DAY,Number,@StartDate) AS DateValue,Number+1 AS Number

FROM master..spt_valuesWHERE number BETWEEN 0 AND 5 AND type = 'P'

),ListOfCategoriesAS(

SELECT Category,Multiplier FROM (VALUES ('Category1',1),('Category2',2),('Category3',3)) AS s (Category,Multiplier)

)

SELECTCategory,DateValue,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN 3 PRECEDING AND CURRENT ROW

) AS CumSumPrev3ValuesInclCurrent --Cumulative SUM of the previous 3 values, including the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN 3 PRECEDING AND 1 PRECEDING

) AS CumSumPrev3ValuesExclCurrent --Cumulative SUM of the previous 3 values, excluding the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

) AS CumSumPrevValuesInclCurrent --Cumulative SUM of all previous values, including the current value

,Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING

) AS CumSumPrevValuesExclCurrent --Cumulative SUM of all previous values, excluding the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN CURRENT ROW AND 3 FOLLOWING

Page 2: Web viewBased on several blog post about all the windows functions

) AS CumSumNext3ValuesInclCurrent --Cumulative SUM of the following 3 values, including the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN 1 FOLLOWING AND 3 FOLLOWING

) AS CumSumNext3ValuesExclCurrent --Cumulative SUM of the following 3 values, excluding the current value

FROM GenerateValues,ListOfCategories

ORDER BYCategory,DateValue

;WITH GenerateValuesAS(

SELECT DATEADD(DAY,Number,@StartDate) AS DateValue,Number+1 AS Number

FROM master..spt_valuesWHERE number BETWEEN 0 AND 5 AND type = 'P'

),ListOfCategoriesAS(

SELECT Category,Multiplier FROM (VALUES ('Category1',1),('Category2',2),('Category3',3)) AS s (Category,Multiplier)

)

SELECTCategory,DateValue,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING

) AS SumPrevCurrentNextValues --Sum of the previous, current and next value.,'||' AS '||',Number*Multiplier AS Value,FIRST_VALUE(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

) AS FirstValuePerCategory --First value of all values per Category,'||' AS '||',Number*Multiplier AS Value,LAST_VALUE(Number*Multiplier) OVER (

PARTITION BY CategoryORDER BY DateValueROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

) AS LastValuePerCategory --Last value of all values per Category (or in this case the hightest value)

,'||' AS '||',Number*Multiplier AS Value,FIRST_VALUE(Number*Multiplier) OVER (

ORDER BY Number*Multiplier ASCROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

) AS LowestValue --First (or in this case lowest) value of all values,'||' AS '||',Number*Multiplier AS Value,LAST_VALUE(Number*Multiplier) OVER (

ORDER BY Number*Multiplier ASCROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

) AS HighestValue --Last (or in this case highest) value of all valuesFROM GenerateValues,

ListOfCategoriesORDER BY

Category,DateValue

;WITH GenerateValuesAS

Page 3: Web viewBased on several blog post about all the windows functions

(SELECT

DATEADD(DAY,Number,@StartDate) AS DateValue,Number+1 AS Number

FROM master..spt_valuesWHERE number BETWEEN 0 AND 5 AND type = 'P'

),ListOfCategoriesAS(

SELECT Category,Multiplier FROM (VALUES ('Category1',1),('Category2',2),('Category3',3)) AS s (Category,Multiplier)

)

SELECTCategory,DateValue,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER () AS SumOfValues --SUM of all values,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

PARTITION BY Category) AS SumOfValuesPerCategory --SUM of all values per Category,'||' AS '||',SUM(Number*Multiplier) OVER (

PARTITION BY Category) AS SumOfValuesPerCategory --SUM of all values per Category,SUM(Number*Multiplier) OVER (

ORDER BY Category ASC) AS SumOfValuesOrderByCategoryAsc --SUM of all values ordered by Category ASC,'||' AS '||',SUM(Number*Multiplier) OVER (

PARTITION BY Category) AS SumOfValuesPerCategory --SUM of all values per Category,SUM(Number*Multiplier) OVER (

ORDER BY Category DESC) AS SumOfValuesOrderByCategoryDesc --SUM of all values ordered by Category DESC,'||' AS '||'

FROM GenerateValues,ListOfCategories

ORDER BYCategory,DateValue

;WITH GenerateValuesAS(

SELECT DATEADD(DAY,Number,@StartDate) AS DateValue,Number+1 AS Number

FROM master..spt_valuesWHERE number BETWEEN 0 AND 5 AND type = 'P'

),ListOfCategoriesAS(

SELECT Category,Multiplier FROM (VALUES ('Category1',1),('Category2',2),('Category3',3)) AS s (Category,Multiplier)

)

SELECTCategory,DateValue,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

ORDER BY DateValueROWS UNBOUNDED PRECEDING

) AS CumSumByRows --Cumulative SUM of all previous values by ROW, including the current value,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER (

ORDER BY DateValue

Page 4: Web viewBased on several blog post about all the windows functions

RANGE UNBOUNDED PRECEDING) AS CumSumByRange --Cumulative SUM of all previous values by RANGE, including the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER () AS SumOfValues --SUM of all values,SUM(Number*Multiplier) OVER (

ORDER BY DateValue, CategoryROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

) AS RevCumSumByRows --Reversed Cumulative SUM of all previous values by ROW, including the current value

,'||' AS '||',Number*Multiplier AS Value,SUM(Number*Multiplier) OVER () AS SumOfValues --SUM of all values,SUM(Number*Multiplier) OVER (

ORDER BY DateValueRANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING

) AS RevCumSumByRange --Reversed Cumulative SUM of all previous values by RANGE, including the current value

FROM GenerateValues,ListOfCategories

ORDER BYDateValue,Category