2016-08-03 10 views
0

私はかなり変わった問題があります。同じIDの行を1つの大きな行に連結したいと考えています。私の問題を説明するために、例を挙げておきます。ここでは、クエリです:PostgreSQL:行を連結する方法

SELECT b.id AS "ID", 
     m.content AS "Conversation" 
FROM bookings b 
INNER JOIN conversations c on b.id = c.conversable_id AND c.conversable_type = 'Booking' 
INNER JOIN messages m on m.conversation_id = c.id 
WHERE b.state IS NOT NULL 
GROUP BY 1,2 
LIMIT 1000; 

そして、ここで出力されます:

ID  **Conversation 
1223 "blah, blah, blah, blah" 
1223 " ... blaaaah, blah.." 
1223 "more blaaah" 
1223 "last blah" 
5000 "new id, with more blah" 
5000 "and the blah continues" 

IDを維持しながら、1つの集約列に会話行を連結する方法はありますか?このよう

ID  Conversation 
1223 "blah, blah, blah, blah, ... blaaaah blah.. more blaaah, last blah" 
5000 "new id, with more blah and the blah continues" 

私はこれを行うための効率的な方法があると確信しています。私は自分でそれを理解することはできません。

+0

'group_concat()'はあなたの友人です。 – wildplasser

+0

それはMySQLではないですか? – DBE7

+0

できます。多分 'string_agg()'でしょうか? – wildplasser

答えて

0

this questionの鮮やかな答えを見て、私自身の問題を解決することができました。これは、PostgreSQL string_agg() -functionを使用するのと同じくらい簡単でした。

関連する問題