0

I have a table that generally looks like:

Name varchar       | Location varchar[]
---------------------------------------------
Lemoney    | {Home,Work,Home,Stack Exchange,,,,,,}
Other Dude | {Home,,,,,,,,,}
Busy Dude  | {Home,Work,Work,Work,Home,Work,,,,}

I have been trying to write a query that will return the Last Value for each Name like:

Lemony     | Stack Exchange
Other Dude | Home
Busy Dude  | Work

I haven't been able to figure out how to return that yet. Does anyone have any ideas?

2
  • Is location an array column, e.g. text[]? Commented Apr 13, 2016 at 13:53
  • VARCHAR[] my apologies.. fixing question Commented Apr 13, 2016 at 13:55

1 Answer 1

0
SELECT distinct Name, loc 
FROM (
  SELECT  *, last_value(Location) over (partition by Name) as loc 
    FROM (SELECT Name, unnest(Location) as Location FROM table)  x) y;

Should answer your question

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.