Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an hstore from a table in postgresql?

I have a query which returns a table of keys and values, thus:

 key |       value        
-----+------------------
   a | 3.73333333333333
   b | 2.3
   c | 2.76666666666667

I'd like to convert it into an hstore:

       hstore         
-----------------------
 'a' => '3.73333333333333', 'b' => '2.3', 'c' => '2.76666666666667'

There are hstore-producing functions that take text, a pair of texts, or even a pair of arrays, but none that take a table in this fashion.

How is this done?

like image 606
MrTheWalrus Avatar asked Feb 15 '26 23:02

MrTheWalrus


1 Answers

Haven't tested, but would this work?

SELECT hstore(array_agg(key ORDER BY key), array_agg(value ORDER BY key))
FROM yourtable
like image 77
Mark Byers Avatar answered Feb 18 '26 16:02

Mark Byers



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!