我有一个包含〜百万个条目的表,如下所示:
customer_id | purchased_at | product
1 | 2012-06-01 00:00 | apples
1 | 2012-09-02 00:00 | apples
1 | 2012-10-01 00:00 | pears
2 | 2012-06-01 00:00 | apples
2 | 2012-07-01 00:00 | apples
3 | 2012-09-02 00:00 | pears
3 | 2012-10-01 00:00 | apples
3 | 2012-10-01 01:00 | bananas
Run Code Online (Sandbox Code Playgroud)
我想将产品连接到一行,DISTINCT和purchase_at的顺序
在MySQL中我只是使用
select customer_id, min(purchased_at) as first_purchased_at,
group_concat(DISTINCT product order by purchased_at) as all_purchased_products
from purchases group by customer_id;
Run Code Online (Sandbox Code Playgroud)
要得到
customer_id | first_purchased_at | all_purchased_products
1 …
Run Code Online (Sandbox Code Playgroud)