Skip to content
Advertisement

Presto lag dates, group/partitioned by id

Say that I want to find every time that a client updated their budget.

Here’s what my data looks like

And the code I’ve run.

What I’m expecting returned will be

Hence there are NULL values for dt_2 in the first entry of each client_id. I’m not sure what code will accomplish this effect; is a GROUP BY clause will be necessary (or a partition over clause.)

But here’s the output to the SQL that I ran

So the huge issue here is that it’s not recognizing that the dt_2 should be NULL if the previous row is from a different client_id.

Which syntax is recommended to accomplish this effect?

Advertisement

Answer

You need to partition by client_id:

The PARTITION BY clause separates the input rows into different partitions. This is analogous to how the GROUP BY clause separates rows into different groups for aggregate functions. If PARTITION BY is not specified, the entire input is treated as a single partition.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement