stahyz
stahyz

Reputation: 101

how to group by column a but order by colum b in postgresql

I trying to group one column and order by another column my data is carts

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

and i expect result like this

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

my sql that i write is not working

select group_id, product_id, created_date from carts group by group_id, product_id, created_date order by created_date

Upvotes: 1

Views: 35

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14269

You need to order by 2 columns - first the group, then the creation date:

SELECT id, product_id, created_date, group_id
FROM table
ORDER BY group_id, created_date

Upvotes: 2

Related Questions