0

Assume I have following table, plus some data.

create table "common"."log"("id" bigserial primary key, 
                            "level" int not null default 0);

Now I have this select query that would return something like this.
select * from common.log where id=147;

+------+--------+
|id    |level   |
+------+--------+
|147   |1       |
|147   |2       |
|147   |2       |
|147   |6       |
|147   |90      |
+------+--------+

Now I like to have something like following rather above

+------+---------------+
|id    |arr_level      |
+------+---------------+
|147   |{1,2,2,6,90}   |
+------+---------------+

So is there any implicit select clause/way for doing this? thanks.

pgsql v9.3

2 Answers 2

1

You can user array function like this

 Select '147' as id,array(select level from common.log where id=147) as arr_level;
Sign up to request clarification or add additional context in comments.

Comments

1

Another way, probably more useful if you have more than one id to query:

SELECT id, array_agg(level) FROM common.log GROUP BY id;

See: aggregate functions.

Comments

Your Answer

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