postgresql:SQL查询使用另一列合并多行中的内容

San*_*ija 3 postgresql merge multiple-columns

我有history以下列的表:

row_id, msgid, sender, receiver, chatcontent, transdate, transtime
Run Code Online (Sandbox Code Playgroud)

每个聊天记录都存储在表格中的单独行中.

场景:如果内容超过2048个字符,我的框架将聊天内容分成多个脚本,数据存储在具有相同细节的多行中,如msgid,sender,receiver,transdate和transtime,只有chatcontent与row_id不同.

例如,我在表格中的内容是

001, msgid1, mark@test.int, james@test.int, this is a long tes, 2013-03-13, 13:55:34
002, msgid1, mark@test.int, james@test.int, t message which is, 2013-03-13, 13:55:34
003, msgid1, mark@test.int, james@test.int,  splitted in multi, 2013-03-13, 13:55:34
004, msgid1, mark@test.int, james@test.int, ple rows, 2013-03-13, 13:55:34
005, msgid2, james@test.int, mark@test.int, yup i got you, 2013-03-13, 13:56:12
Run Code Online (Sandbox Code Playgroud)

现在我想在单个查询中获取数据,该查询应该具有输出

msgid1, mark@test.int, james@test.int, this is a long test message which is splitted in multiple rows, 2013-03-13, 13:55:34
msgid2, james@test.int, mark@test.int, yup i got you, 2013-03-13, 13:56:12
Run Code Online (Sandbox Code Playgroud)

怎么做.我无法获取单个查询中的所有详细信息.我能够使用命令在一列中合并chatcontent,但我不想硬编码msgid的值

SELECT array_to_string(array(SELECT chatcontent FROM history where msgid='msgid1'),'');
Run Code Online (Sandbox Code Playgroud)

Clo*_*eto 9

SELECT
    msgid,
    sender,
    receiver,
    transdate,
    transtime,
    array_to_string(array(
        select chatcontent
        from history
        where msgid = h.msgid
        order by row_id
        ), ' '
    ) chatcontent
from history h
group by 1, 2, 3, 4, 5, 6
order by 1
Run Code Online (Sandbox Code Playgroud)