使用PostgreSQL 9.0.
比方说,我有一个包含字段的表:company,profession和year.我想返回一个包含独特公司和专业的结果,但基于数字序列聚合(到一个数组中很好)年份:
示例表:
+-----------------------------+
| company | profession | year |
+---------+------------+------+
| Google | Programmer | 2000 |
| Google | Sales | 2000 |
| Google | Sales | 2001 |
| Google | Sales | 2002 |
| Google | Sales | 2004 |
| Mozilla | Sales | 2002 |
+-----------------------------+
Run Code Online (Sandbox Code Playgroud)
我对一个输出类似于以下行的查询感兴趣:
+-----------------------------------------+
| company | profession | year |
+---------+------------+------------------+
| Google | Programmer | [2000] |
| …Run Code Online (Sandbox Code Playgroud) 基本上我有一个表messages,其中user_id包含标识创建消息的用户的字段.
当我在两个用户之间显示一个对话(一组消息)时,我希望能够user_id按照一种棘手的方式对消息进行分组:
假设有一些消息(排序依据created_at desc):
id: 1, user_id: 1
id: 2, user_id: 1
id: 3, user_id: 2
id: 4, user_id: 2
id: 5, user_id: 1
Run Code Online (Sandbox Code Playgroud)
我希望按以下顺序获得3个消息组:
[1,2], [3,4], [5]
它应该按*user_id*分组,直到它看到另一个,然后按那个分组.
我正在使用PostgreSQL,并乐意使用特定的东西,无论什么能带来最佳性能.