如果表格包含以下数据
num | date_ | s | i_id
-------+-------------+------------------------+---------
1 | 2013-12-12 | (2,1,2013-12-12,80.56) | 2
1 | 2013-12-12 | (3,1,2013-12-12,70.56) | 3
1 | 2013-12-10 | (4,1,2013-12-10,90.76) | 4
2 | 2013-12-10 | (5,2,2013-12-10,90.76) | 5
2 | 2013-12-06 | (6,2,2013-12-06,90.76) | 6
3 | 2013-12-06 | (7,3,2013-12-06,90.76) | 7
3 | 2013-12-06 | (8,3,2013-12-06,90.76) | 8
我想要一个查询,它会为具有相同num的不同日期的记录提供num,i_id。 它应该返回num-1,2和相应的i_id。 我该怎么办?
答案 0 :(得分:0)
select num, group_concat(i_id) as iids
from your_table
group by num
having count(distinct date_) > 1
答案 1 :(得分:0)
这应该适用于PostgreSQL,版本号> gt = = 9.0
SELECT
num
, array_to_string(array_agg(i_id ORDER BY num), ',')
FROM a
GROUP BY num
参见演示http://sqlfiddle.com/#!15/25502/5
在您的评论中,您希望获得MAX(日期_)
SELECT
num
, MAX(date_)
, array_to_string(array_agg(i_id ORDER BY num), ',')
FROM a
GROUP BY num