Postgresql group by

Good day!
Is the order table: id | customer_id | created | category_id
There is the query select customer_id, date_trunc('day', min("order".created)) as created from "order" group by 1
He regularly selects the data that should, in fact, the user ID with the minimum date of his order.
You need to pull out even category_id
That is
select customer_id, date_trunc('day', min("order".created)) as created, category_id from "order" group by 1,3
this query gives a side-line, it is necessary to select the category_id for the order in which min("order".created)
Tell me how?
October 8th 19 at 00:09
4 answers
October 8th 19 at 00:11
Using DISTINCT might work:

select distinct on (customer_id) customer_id, date_trunc('day', order.created) as created, category_id from "order" order by 1, order.created DESC
October 8th 19 at 00:13
group by in this case reduces as dB is, as I understand it, you probably need to look in the direction of having. www.postgresql.org/docs/8.1/static/tutorial-agg.html
October 8th 19 at 00:15
select category_id order in which min("order".created)

select customer_id, created, category_id 
from 
 (select customer_id, date_trunc('day', order.created) as created, category_id 
 ,row_number() over (partition by customer_id order by date_trunc('day', order.created)) rn
 from "order")s
where rn = 1
October 8th 19 at 00:17
select distinct on(customer_id) customer_id,category_id,date_trunc('day', min(created)) as createdon from testtable group by customer_id,category_id order by customer_id,createdon

Find more questions by tags PostgreSQL