1

I want to treat an array of timestamps as a set of records that could be related with other tables.

For example:

SELECT array[0], COUNT(b.id) FROM array, B WHERE B.date > array[0]

What's the best way for achieving something like this?

1 Answer 1

2

It sounds like you are looking for the unnest function.

regress=> SELECT arraycol 
          FROM unnest(ARRAY[1,2,3,4,5]) arraycontent(arraycol);
 arraycol 
--------
      1
      2
      3
      4
      5
(5 rows)

You can join on the array's contents; unnest, being a set-returning function, can be used like any other FROM term.

If your PostgreSQL is too old to have unnest then it's too old to run, too. Start planning an upgrade.

Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't recognize the arraycontent function, I can't use the column as a normal column which I can use in WHERE clause.
@william arraycontent isn't a function, it's a table-alias. "label the results of the unnest function 'arraycontent' with single column 'arraycol' for reference elsewhere"

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.