Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - When would an empty OVER clause be used?

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.

like image 327
Brian Webster Avatar asked Jul 28 '10 16:07

Brian Webster


People also ask

Can over () be empty in SQL?

over() (empty over clause) The over clause can be empty, meaning over all rows of the result. Returns the original result with an additional column stating how many rows the result has.

What is use of over clause in SQL?

Determines the partitioning and ordering of a rowset before the associated window function is applied. That is, the OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window.

When using analytic function if the over clause is empty?

If your query uses an analytic function like SUM(x) and you specify an empty OVER() clause, the analytic function is used as a reporting function, where the entire input is treated as a single partition; the aggregate returns the same aggregated value for each row of the result set.

Why we use over partition by in SQL Server?

It's recommended to use the SQL PARTITION BY clause while working with multiple data groups for the aggregated values in the individual group. Similarly, it can be used to view original rows with the additional column of aggregated values.


2 Answers

Say our table is employees:

+-----------+-------+---------+
| badge_num | name  | surname |
+-----------+-------+---------+
|         1 | John  | Smith   |
|         2 | Mark  | Pence   |
|         3 | Steve | Smith   |
|         4 | Bob   | Smith   |
+-----------+-------+---------+

Running

SELECT surname, COUNT(*)
FROM employees
GROUP BY surname;

we'll get:

+---------+----------+
| surname | COUNT(*) |
+---------+----------+
| Smith   |        3 |
| Pence   |        1 |
+---------+----------+

While running

SELECT surname, COUNT(*) OVER()
FROM employees
GROUP BY surname;

we'll get:

+---------+-----------------+
| surname | COUNT(*) OVER() |
+---------+-----------------+
| Smith   |               2 |
| Pence   |               2 |
+---------+-----------------+

In the second case, in each row we are just counting the number of rows of the whole select (not the single partition).

like image 158
horcrux Avatar answered Nov 08 '22 08:11

horcrux


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

like image 38
Michael Pakhantsov Avatar answered Nov 08 '22 07:11

Michael Pakhantsov