The Windows aggregate functions are normal aggregate functions, the normal aggregate functions have some limitations.
In the normal aggregate functions we need to use a Group by clause or hide some of the columns.
The normal aggregate functions like Sum, Max, Min, Avg and Count can return the grouped data, but in the Windows aggregate functions they can return the row by row aggregated data.
Demo
- CREATE TABLE DBO.SALES (
- PROD_ID INT, SALES_YEAR INT, SALES_AMOUNT INT
- ) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2009, 10000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2010, 9000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2011, 8000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2012, 7000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2013, 14000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2014, 18000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (1, 2015, 15000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (2, 2013, 12000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (2, 2014, 8000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (2, 2015, 16000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (3, 2012, 7000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (3, 2013, 8000) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (3, 2014, 9700) INSERT INTO DBO.SALES(
- PROD_ID, SALES_YEAR, SALES_AMOUNT
- )
- VALUES
- (3, 2015, 12500)
- SELECT
- *
- FROM
- DBO.SALES

- --SUM OF SALES TOTAL TO ALL ROWS USING WINDOW AGGREGATE FUNCTION
- SELECT *, FORMAT(SUM(SALES_AMOUNT) OVER(),'C') [SUM_TOTALSALES] FROM DBO.SALES
- SELECT *, (SELECT FORMAT(SUM(SALES_AMOUNT) ,'C') FROM DBO.SALES) AS [SUM_TOTALSALES] FROM DBO.SALES

- --MAX OF SALES TOTAL FROM ROWS TO ALL ROWS USING WINDOW AGGREGATE FUNCTION
- SELECT *, MAX(SALES_AMOUNT) OVER() [MAXLSALES] FROM DBO.SALES

We can also specify the partition clause in the over function.
- --SUM OF SALES TOTAL TO ALL ROWS USING WINDOW AGGREGATE FUNCTION WITH PARTITION BY
- SELECT
- *,
- SUM(SALES_AMOUNT) OVER(PARTITION BY PROD_ID) [SUM_TOTALSALES_WITHPARTITIONBY]
- FROM
- DBO.SALES

- SELECT
- PROD_ID,
- SALES_YEAR,
- FORMAT(SALES_AMOUNT, 'C') AS SALES_AMOUNT,
- FORMAT(
- SUM(SALES_AMOUNT) OVER(PARTITION BY PROD_ID),
- 'C'
- ) [SUM_PROD_TOTALSALES],
- FORMAT(
- CAST(SALES_AMOUNT AS FLOAT)/ SUM(
- CAST(SALES_AMOUNT AS FLOAT)
- ) OVER(PARTITION BY PROD_ID),
- 'P'
- ) AS [PERCENT_PROD]
- FROM
- DBO.SALES

- SELECT
- PROD_ID,
- SUM(SALES_AMOUNT) AS [PROD_TOTAL_SALES],
- SUM(
- SUM(SALES_AMOUNT)
- ) OVER(
- ORDER BY
- SUM(SALES_AMOUNT)
- ) AS [ALL_GROUP_RUNNING_TOTAL]
- FROM
- DBO.SALES
- GROUP BY
- PROD_ID

Window aggregate functions can support Window frame clause.
Window frame clause can be:
- UNBOUNDED PRECEDING OR FOLLOWING: from the beginning or ending of the rows-based partition by clause.
- CURRENT ROW: the current row.
- N ROWS PRECEEDING OR FOLLOWING: N rows before or after.

When we define a Windows aggregate function if we provide an order by clause without a frame clause then by default SQL Server provides RANGE BETWEEN UNBOUNDED PRECEEDING AND CUURENT ROW.
- SELECT *, SUM(SALES_AMOUNT) OVER( PARTITION BY PROD_ID ORDER BY SALES_AMOUNT ) [SUM_TOTALSALES_WITHPARTITIONBY] FROM DBO.SALES

The following shows the Sum of previous running totals with a partition by clause:
- SELECT *, SUM(SALES_AMOUNT) OVER( PARTITION BY PROD_ID ORDER BY SALES_AMOUNT
- ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) [SUM_TOTALSALES_WITHPARTITIONBY] FROM DBO.SALES
- SELECT *, SUM(SALES_AMOUNT) OVER( PARTITION BY PROD_ID ORDER BY SALES_AMOUNT
- ROWS UNBOUNDED PRECEDING ) [SUM_TOTALSALES_WITHPARTITIONBY] FROM DBO.SALES

The following is a sample of Sum of the next running totals with a partition by clause:
- SELECT *, SUM(SALES_AMOUNT) OVER( PARTITION BY PROD_ID ORDER BY SALES_AMOUNT
- ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) [SUM_TOTALSALES_WITHPARTITIONBY] FROM DBO.SALES

The following is a sample of Sum of the Previous 2 rows totals and the current row:
- SELECT *, SUM(SALES_AMOUNT) OVER( PARTITION BY PROD_ID ORDER BY SALES_AMOUNT
- ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ) [SUM_TOTALSALES_WITHPARTITIONBY] FROM DBO.SALES

Rakesh KalluriPosted Jul 29, 2015, 7:47 AM
Thanks sibeesh venu
Sibeesh VenuPosted Jul 29, 2015, 6:09 AM
Nice Share.
Rakesh KalluriPosted Jul 29, 2015, 3:04 AM
Thanks Debasis Saha
Debasis SahaPosted Jul 29, 2015, 2:04 AM
Nice Article..
Rakesh KalluriPosted Jul 29, 2015, 1:58 AM
Thanks Rajeesh Menoth.
Rakesh KalluriPosted Jul 29, 2015, 1:58 AM
Thanks Nilesh Jadav
Rakesh KalluriPosted Jul 29, 2015, 1:58 AM
Thanks Rakesh Chavda
Rakesh KalluriPosted Jul 29, 2015, 1:57 AM
Thanks Gopi Chand
Rajeesh MenothPosted Jul 29, 2015, 12:17 AM
Good One...
Nilesh JadavPosted Jul 29, 2015, 12:05 AM
NIce article sir
RakeshPosted Jul 28, 2015, 11:20 PM
Good one
Gopi ChandPosted Jul 28, 2015, 11:10 PM
Nice one
Rakesh KalluriPosted Jul 28, 2015, 10:19 PM
Thanks van lai
Rakesh KalluriPosted Jul 28, 2015, 10:19 PM
Thanks santhakumar .
Van LaiPosted Jul 28, 2015, 10:15 PM
Nice article.
Santhakumar MunuswamyPosted Jul 28, 2015, 3:08 PM
Good. Thanks for sharing