How to Use the LEAD() Function in SQL Server

In SQL Server, LEAD() is a window function that allows us to access a value from a later row in the same result set, without the need to perform a self-join.

We specify the row as an offset from the current row. An offset of 1 means it gets the value from the next row, an offset of 2 means two rows forward, and so on.

Continue reading

Fix Error “The function ‘ROW_NUMBER’ must have an OVER clause with ORDER BY” in SQL Server

If you’re getting an error that reads “The function ‘ROW_NUMBER’ must have an OVER clause with ORDER BY” in SQL Server, it’s probably because you’re calling the ROW_NUMBER() function without an ORDER BY clause.

Window functions such as ROW_NUMBER() require an OVER clause, and that clause must have an ORDER BY clause. If you’re getting the above error, there’s a good chance you’re providing an OVER clause, but you’re omitting the ORDER BY clause.

To fix this issue, add an ORDER BY clause to the OVER clause when calling the ROW_NUMBER() function.

Continue reading

Introduction to the LAG() Function in SQL Server

In SQL Server, LAG() is a window function that enables us to access a value from a previous row in the same result set, without the need to perform a self-join.

We specify the previous row as an offset from the current row. An offset of 1 means the previous row, an offset of 2 means two rows back, and so on.

Continue reading

How to Use the LAST_VALUE() Function in SQL Server

In SQL Server, the LAST_VALUE() function returns the last value in an ordered set of values.

LAST_VALUE() is a window function that enables us to get a value from the last row of a query result set or partition. This can be useful for when we want to do stuff such as compare a value from the current row with a value in the last row or include it in a calculation.

You may need to explicitly set the window frame if you want LAST_VALUE() to return the actual last value from the partition or result set. That’s because the default window frame ends with the current row. This is covered in the example below.

Continue reading

How to Use the FIRST_VALUE() Function in SQL Server

In SQL Server, FIRST_VALUE() is a window function that returns the first value in an ordered set of values.

Basically, we can use it to get a value from the first row of a query result set or partition. This can be handy if we want to compare a value in the current row with a value in the first row, or use it as part of a calculation.

Continue reading

Fix Error “function ntile() does not exist” in PostgreSQL

If you’re getting an error that reads “function ntile() does not exist” in PostgreSQL, it could be that you’re calling the ntile() function without any arguments.

This error message could be a bit misleading. The ntile() function does in fact exist (if you’re using PostgreSQL 8.4 or later), but the error occurs when we don’t provide an argument, or if we provide an argument of the wrong type.

Continue reading

Calculate the Difference Between the Current Row and a Following Row in MySQL

In MySQL, we can use the LEAD() function to get the value of a subsequent row. For example we can get a value from the next row, or the one after that, and so on.

This enables us to do things like compute the difference between a value in the current row and a value in a following row. We can do this even if both values are in the same column.

Continue reading

How to Fix Error “window function nth_value requires an OVER clause” in PostgreSQL

If you’re getting an error that reads “window function nth_value requires an OVER clause” in PostgreSQL, it’s because you’re calling the nth_value() function without an OVER clause.

PostgreSQL requires that you include an OVER clause any time you call a built in nonaggregate window function such as nth_value().

To fix this issue, add an OVER clause to your window function.

Continue reading