[英]How to select the key of a group order by column a where a column b is not ordering
[英]how to group by column a but order by colum b in postgresql
我試圖將一列分組並按另一列排序我的數據是購物車
id product_id created_date group_id
1. 34 17:00:01 1
2. 35 17:00:02 1
3. 36 17:00:03 2
4. 37 17:00:04 1
5. 38 17:00:05 2
我期待這樣的結果
id product_id created_date group_id
1. 34 17:00:01 1
2. 35 17:00:02 1
4. 37 17:00:04 1
3. 36 17:00:03 2
5. 38 17:00:05 2
我寫的 sql 不工作
select group_id, product_id, created_date from carts group by group_id, product_id, created_date order by created_date
您需要按 2 列排序 - 首先是組,然后是創建日期:
SELECT id, product_id, created_date, group_id
FROM table
ORDER BY group_id, created_date
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.