Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a query result to hstore

I am using PostgreSQL 9.4. I need to convert subquery to hstore. I have this query: select code, value_string from dir. It returns

code  | value_string
------|--------------
CODE  | 1
ACC   | 2
...

How to convert his result to hstore('"ACC"=>"2", "CODE"=>"1", ...')?

I am looking something like this: SELECT hstore(select code, value_string from dir).

like image 732
dev.brutus Avatar asked Nov 02 '25 15:11

dev.brutus


1 Answers

For the documentation:

hstore(text[], text[]) - construct an hstore from separate key and value arrays.

Use array_agg() as arguments for this function. Example:

create table dir (code text, value_string text);
insert into dir values
('CODE', 1),
('ACC', 2);

select hstore(array_agg(code), array_agg(value_string))
from dir;

         hstore          
-------------------------
 "ACC"=>"2", "CODE"=>"1"
(1 row)
like image 177
klin Avatar answered Nov 04 '25 11:11

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!