Using OVER() with Aggregate Functions in Sql Server 2005

1.you can now add aggregate functions to any SELECT (even without a GROUP BY clause) by specifying an OVER() partition for each function.

2.SUM(..) OVER(..) is most useful for calculating a Total or percentage of a total for each row

3.SUM() function works with any of the other aggregate functions as well, such as MIN() or AVG()

Partition by

Divides the result set into partitions. The window function is applied to each partition separately and computation restarts for each partition.

select
StudentId,
StudentName,
Subject,
Mark,
sum(Mark) OVER (Partition by StudentId) as Total
from
Student

Output
=====
StudentId StudentName Subject Mark Total
======= ========== ===== ==== =====
1 Raja Physics 80 240
1 Raja Maths 90 240
1 Raja Biology 70 240
2 Sam Physics 90 150
2 Sam Maths 60 150

Popularity: 3% [?]

You can leave a response, or trackback from your own site.

Leave a Reply

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars