How to Fix Error “The function ‘CUME_DIST’ must have an OVER clause” in SQL Server

If you’re getting an error that reads “The function ‘CUME_DIST’ must have an OVER clause” when using SQL Server, it’s because you’re calling the CUME_DIST() function without an OVER clause.

The CUME_DIST() function requires an OVER clause (and that clause must have an ORDER BY clause).

To fix this issue, be sure to include an OVER clause when calling the CUME_DIST() function.

Continue reading

Get the Most Populous City in Each District When Using a Window Function in SQL

While it’s true that we can use the SQL MAX() function to get the maximum value in a column, what if we want to return the value from another column instead of the actual maximum value itself? And what if we want it partitioned by category, so that it’s based on the maximum value from each category? And what if we want all values listed out, including those that aren’t the most populous?

In the following example, we use a window function to solve this problem.

Continue reading