我如何一起使用SQL GroupBy和Concatenate?

时间:2015-08-28 16:22:42

标签: sql group-by concatenation

目前有:

Time, Interaction, CustomerId  

1/5/2015, Facebook, 1    
1/12/2015, Mail, 3  
1/3/2015, Email, 1  
1/9/2015, Facebook, 1  
1/17/2015, Mail, 4  
1/1/2015, Mail, 1  

想要:

CustomerId, Interaction Path, Date Path  
1, Mail > Email > Facebook > Facebook, 1/1/2015 > 1/3/2015 > 1/5/2015 > 1/9/2015  
3, Mail, 1/12/2015  
4, Mail, 1/17/2015  

因此,交互路径和日期路径将按从最旧到最近的日期的顺序排列。

非常感谢您阅读。

2 个答案:

答案 0 :(得分:1)

在SQL Server中:

create table my_table (Time date, Interaction varchar(50), CustomerId  int);

insert into my_table values
(N'1/5/2015',  'Facebook', 1 ),   
(N'1/12/2015', 'Mail'    , 3  ),
(N'1/3/2015',  'Email'   , 1  ),
(N'1/9/2015',  'Facebook', 1 ), 
(N'1/17/2015', 'Mail', 4  ),
(N'1/1/2015',  'Mail', 1  );

使用STUFFXML Path,您可以轻松完成此操作:

SELECT q1.Customerid + ', ' 
       + replace(q1.Interaction, ',', ' > ') 
       + ', ' + replace(q1.[Time], ',', ' > ') AS desired_output
FROM (
    SELECT DISTINCT cast(t2.CustomerId AS VARCHAR(10)) AS CustomerId
        ,STUFF((
                SELECT ',' + t1.Interaction
                FROM my_table T1
                WHERE T1.CustomerId = T2.CustomerId
                ORDER BY t1.[time]
                FOR XML PATH('')
                ), 1, 1, '') AS Interaction
        ,STUFF((
                SELECT ',' + cast(t1.[Time] AS VARCHAR(255))
                FROM my_table T1
                WHERE T1.CustomerId = T2.CustomerId
                ORDER BY t1.[time]
                FOR XML PATH('')
                ), 1, 1, '') AS [Time]
    FROM my_table T2
    ) q1;

<强>结果:

+------------------------------------------------------------------------------------------+
|                        CustomerId, Interaction Path, Date Path                           |
+------------------------------------------------------------------------------------------+
| 1, Mail > Email > Facebook > Facebook, 2015-01-01 > 2015-01-03 > 2015-01-05 > 2015-01-09 |
| 3, Mail, 2015-01-12                                                                      |
| 4, Mail, 2015-01-17                                                                      |
+------------------------------------------------------------------------------------------+

SQL Fiddle Demo

注意:如果您需要与您的日期格式相同的日期,则可以将Time字段转换为日期样式101

答案 1 :(得分:0)

MySQL解决方案:

使用GROUP_CONCAT(expr)功能。 可以在此处找到文档:https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

<强>解决方案

select CustomerID, GROUP_CONCAT(interaction ORDER BY time SEPARATOR '>') as 'Interaction Path', GROUP_CONCAT(time ORDER BY time SEPARATOR '>') as 'Date Path' from my_table group by CustomerID;

<强>结果

+------------+------------------------------+---------------------------------------------+
| CustomerID | Interaction Path             | Date Path                                   |
+------------+------------------------------+---------------------------------------------+
|          1 | Mail>Email>Facebook>Facebook | 2015-01-01>2015-01-03>2015-01-05>2015-01-09 |
|          3 | Mail                         | 2015-01-12                                  |
|          4 | Mail                         | 2015-01-17                                  |
+------------+------------------------------+---------------------------------------------+