当Oracle中的id相同时连接记录

时间:2014-10-28 19:10:24

标签: oracle oracle11g

我有Oracle 11 G数据库。

我的桌子CHATUSERS看起来像这样:

UserId    - Identifier
SeqNum    - Order of chat conversation
ChatText  - Chat Text
DateAdded - DateTime Chat conversation happened.

用户一天可以有多个聊天会话。 我想每人每天组合所有ChatText。

示例:

UserId SeqNum DateAdded ChatText 
--------------------------------
1      1      28-OCT-14 Hey, How are You?
1      2      28-OCT-14 Do you have a minute?
1      3      28-OCT-14 Wanted to talk to you about something.
1      1      25-OCT-14 Congratulations!
1      2      25-OCT-14 on your promotion

因此SELECT查询会将结果返回为:

UserId, DateAdded and CompleteText
1  28-OCT-14 Hey, How are You? Do you have a minute? Wanted to talk to you about something.
1  25-OCT-14 Congratulations! on your promotion

我该怎么做?

2 个答案:

答案 0 :(得分:0)

select userid, dateadded, listagg(chattext, ' ') within group (order by seqnum) 
from chatusers
group by userid, dateadded;

答案 1 :(得分:0)

如果您在11gR2,则可以使用LISTAGG

SELECT UserId, 
                DateAdded, 
                LISTAGG(ChatText, ',') WITHIN GROUP (ORDER BY ChatText) AS CompleteText
FROM  ChatUsers
GROUP BY UserId, DateAdded;

对于其他选择,请查看http://oracle-base.com/articles/misc/string-aggregation-techniques.php