5

I am using Postgresql 8.4. I have a table like this:

 type | value
------+-------
 1    | 5
 2    | 6
 1    | 4
 3    | 10

I want to write one select that will give me the minimum & maximum value, and an aggregate of all the types as integer[]. The desired result should be:

 min | max | types
-----+-----+-----------
 4   | 10  | {1, 2, 3}

To get the min and max, I already have:

SELECT MIN(value) min, MAX(value) max FROM table;

To get the types in a standalone select, I use:

SELECT array_agg(DISTINCT type) types FROM table;

How can I combine these into one select (that is not too inefficient)?

1
  • 3
    Doesn't SELECT MIN(value) min, MAX(value) max, array_agg(DISTINCT type) types FROM table; work for you? Commented Mar 11, 2014 at 7:57

2 Answers 2

10
SELECT array_agg(DISTINCT type) AS types,
       MIN(value) AS min,
       MAX(value) AS max
FROM your_table
Sign up to request clarification or add additional context in comments.

Comments

2

Try this query:

SELECT * FROM
(SELECT MIN(value),MAX(value) FROM table1)t1,
(SELECT array_agg(DISTINCT type) FROM table1)t2;

SQL Fiddle

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.