views:

26

answers:

1

I'm analyzing some code that utilizes empty OVER clauses in the contest of Count().

Example:

SELECT 
        ROW_NUMBER() OVER (ORDER BY Priority DESC) AS RowID,
        CAST((COUNT(*) OVER() / @pagesize) AS Int) AS TotalPages,

I'm trying to understand why the empty OVER clause is being used here.

There are other standard select elements below those two lines I listed above, and when I remove the empty OVER clause from the second the TotalPages line, I get errors like this:

Column 'TableA.Priority' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

As soon as I put the OVER() back, the error is gone.

My understanding of the OVER clause is very limited... I feel Like I understand what's going on in the RowID line... but the TotalPages line just baffles me.

+3  A: 

OVER() is part of analytic function and define partitions in your recordset. OVER() is just one partition and applied to the whole dataset

i.e. COUNT(*) OVER() will return in each row how many records in your dataset.

look to this http://msdn.microsoft.com/en-us/library/ms189461.aspx

Michael Pakhantsov
So.. this would be a way to get the total records of a select statement, from within the select statement?
hamlin11
@hamlin11, exactly.
Michael Pakhantsov