1

I have a simple dataset like this:

CREATE TABLE test (ts timestamp, email varchar, title varchar, hobby varchar);

insert into test values
('2017-01-01', '[email protected]', 'blah', null),
('2017-01-02', '[email protected]', null, 'expected hobby'),
('2017-01-03', '[email protected]', 'expected title', null),
('2017-01-04', '[email protected]', null, null);

And I'd like to select the most recent non-null value of title and hobby per email, using a window function. These are 'expected title' and 'expected hobby'. I'm trying this:

SELECT DISTINCT
    email,
    first_value(title) OVER (partition by email order by ts desc nulls last),
    first_value(hobby) OVER (partition by email order by ts desc nulls last)
from test

But it doesn't seem to work. Here's a sql fiddle: http://sqlfiddle.com/#!17/6d681/2

Any idea why?

2
  • 1
    I may have solved this with: first_value(title) OVER (partition by email order by case when title is not null then ts end desc nulls last), Commented Mar 13, 2018 at 15:00
  • 1
    It seems to work. You should put it as answer. Commented Mar 13, 2018 at 17:05

2 Answers 2

2

I fixed it using a case statement to eliminate the nulls:

SELECT DISTINCT
    email,
    first_value(title) OVER (partition by email order by case when title is not null then ts end desc nulls last),
    first_value(hobby) OVER (partition by email order by case when hobby is not null then ts end desc nulls last),
from test
Sign up to request clarification or add additional context in comments.

Comments

1

your order does not work as excpected

 order by ts desc nulls last

because ts has no nulls, something like that should also work (as your solution)

SELECT distinct 
    email,
    first_value(title) OVER (partition by email order by (ts || title) desc nulls last ROWS  BETWEEN  UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING ) as title,
    first_value(hobby) OVER (partition by email order by (ts || hobby) desc nulls last ROWS  BETWEEN  UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING ) as hobby
from test 

a concat with null results in null, so ts || null results in null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.