Paulo Fertonani
Paulo Fertonani

Reputation: 33

Lag function does not accept floating point values using postgres SQL

I'm trying to select a double precision (floating) value from a previous line of a table in PostgreSQL, but the lag() function is only working if the chosen column is an integer:

This code gives the error:

"ERROR: function lag(double precision, integer, integer) does not exist"

select id_client, date, value, lag(value, 1, 0) over (order by date)

This code works fine but i loose the decimals :

select id_client, date, value, lag(cast(value as int), 1, 0) over (order by date)

Upvotes: 3

Views: 2741

Answers (2)

JosMac
JosMac

Reputation: 2322

your default in lag function must be of the same type as main value:

lag(value, 1, 0::double precision) then it works

Upvotes: 6

F.Lazarescu
F.Lazarescu

Reputation: 1385

Try like this:

select id_client, date, value, lag(value) over (order by date)

Upvotes: 0

Related Questions