如何从下表中的col3重写数据:
public CommentsAdapter(List<PostComments> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
for (PostComments comments : mPostComments) {
comments.setDate_posted(MethodCalculateTime.calculateAge(mServerTime,
Long.valueOf(comments.getDate_posted())));
}
}
成为
col1 col2 col3
1 1 5
1 2 3
1 2 4
1 2 4
1 2 6
1 1 5
1 2 7
1 3 7
答案 0 :(得分:1)
我建议您将结果放入 array 中,而不要放在单独的列中。为了将数据放在单独的列中,您将需要某种类型的动态SQL。
所以,这也许可以满足您的需求:
select col1, col2, array_agg(col3 order by col3)
from t
group by col1, col2;
答案 1 :(得分:0)
对于固定的最大列数,可以使用row_number()
和条件聚合:
select
col1,
col2,
max(col3) filter(where rn = 1) col3,
max(col3) filter(where rn = 2) col4,
max(col3) filter(where rn = 3) col5,
max(col3) filter(where rn = 4) col6,
max(col3) filter(where rn = 5) col7
from (
select
col1,
col2,
row_number() over(partition by col1, col2 order by col3) rn
from mytable
) t