4

I have the following table One:

 id │ value 
────┼───────
  1 │ a
  2 │ b

And Two:

  id │ value 
─────┼───────
  10 │ a
  20 │ a
  30 │ b
  40 │ a
  50 │ b

One.value has a unique constraint but not Two.value (one-to-many relationship).

Which SQL (Postgres) query will retrieve as array the ids of Two whose value match One.value? The result I am looking for is:

          id │ value 
─────────────┼───────
  {10,20,40} │ a
     {30,50} │ b

1 Answer 1

5

Check on SQL Fiddle

SELECT array_agg(id) AS id, "value"
  FROM Two
 GROUP BY "value";

Using value as identifier (column name here) is a bad practice, as it is a reserved keyword.

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

1 Comment

Thank you for the extra tip about "value"

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.