4
나는 테이블이 포함 된이 ~ 같은 백만 항목 :GROUP_CONCAT
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
내가 한 행, DISTINCT로하고 purchased_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;
를 사용
customer_id | first_purchased_at | all_purchased_products
1 | 2012-06-01 00:00 | apples, pears
2 | 2012-06-01 00:00 | apples
3 | 2012-09-02 00:00 | pears, apples, bananas
SQL Server 2012에서 어떻게 할 수 있습니까?
나는 일 다음 '해킹'을 시도하지만 잔인한의 긴 테이블select
customer_id,
min(purchased_at) as first_purchased_at,
stuff ((select ',' + p3.product
from (select p2.product, p2.purchased_at,
row_number() over(partition by p2.product order by p2.purchased_at) as seq
from purchases p2 where
p2.customer_id = p1.customer_id) p3
where p3.seq = 1 order by p3.purchased_at
for XML PATH('')), 1,1,'') AS all_purchased_products
from purchases p1
group by customer_id;
나는이 문제를 해결하기 위해 무엇을 할 수에서 잘 수행하지 않습니다?