Search code examples
postgresqlhstore

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?


Solution

  • Haven't tested, but would this work?

    SELECT hstore(array_agg(key ORDER BY key), array_agg(value ORDER BY key))
    FROM yourtable