Skip to content
Advertisement

date window function

Dont know how to solve the problem.May be you can show right direction or give a link. I have a table:

I want to aggregate data (id, Date_min) and add new column like this one:

In column Data_new I want to see next user’s first date. If there is no next user, add user`s max date

Advertisement

Answer

LEAD will give you the next date, but we also have the slight sticking problem that your ID repeats, so we need something to make the second 23 distinct from the first. For that I guess we can establish a counter that ticks up every time the ID changes:

I haven’t got a PG instance to test this on, but it works in SQLS and I’m pretty sure that PG supports everything that SQLS does – there isn’t any SQLS specific stuff here

  • a takes the place of your table – you can drop it from your query and just straight d a with b as (select... from yourtablenamehere)
  • b calculates the previous ID; we’ll use this to detect if the id has changed between current row and prev row. If it changes we’ll put a 1 otherwise a 0. When these are summed as a running total it effectively means the counter ticks up every time the ID changes, so we can group by this counter as well as the ID to split our two 23s apart. We need to do this separately because window functions can’t be nested
  • c takes the last_id and does the running total. It also does the next_date with a simple window function that pulls the date from the following row (rows ordered by date). the ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW is techincally unnecessary as it’s the default action for a SUM OVER ORDERBY, but I find being explicit helps document/change if needed
  • then all that is required is to select the id, min date and max next_date, but throw the counter in there too to split the 23s up – you’re allowed to group by more columns than you select but not the other way round
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement