• SqlServer 对分组的内容进行拼接-group_concat


     SqlServer  对分组的内容进行拼接:

    方案1:xml 子集,性能较差

    方案2:借助 sqlCLR 接入。实现group_concat。性能完美,但是 阿里云的不支持!!!!

    CREATE TABLE [dbo].[a_data_test](
    [empid] [INT] NULL,
    [workdate] [DATETIME] NULL
    ) ON [PRIMARY]

    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (1, '2018-8-17 10:20:43.33310:');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (1, '2018-8-17 10:20:43.333');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (2, '2018-8-17 10:21:09.29310:');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (2, '2018-8-17 10:21:09.293');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (3, '2018-8-17 10:21:37.87710:');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (3, '2018-8-17 10:21:37.880');
    GO
    INSERT INTO [a_data_test] ([empid], [workdate]) VALUES (3, '2018-8-17 10:21:37.88010:');
    GO

     1:

    SELECT
    ta.empid,
     STUFF((SELECT ',' +CONVERT(varchar(100), b.workdate, 25) 
                             FROM a_data_test b
                             WHERE ta.empid=b.empid
                             FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
    FROM 
    (
    SELECT 
    a.empid
     FROM
    dbo.a_data_test a
    GROUP BY a.empid
    ) ta
    --LEFT JOIN dbo.a_data_test b ON b.empid = ta.empid
    

      

    2: 

     https://archive.codeplex.com/?p=groupconcat
    https://github.com/orlando-colamatteo/ms-sql-server-group-concat-sqlclr/tree/master/GroupConcat 
    https://www.codeproject.com/Articles/691102/String-Aggregation-in-the-World-of-SQL-Server
    https://www.anexinet.com/blog/group_concat-functionality-for-sql-server/
     
  • 相关阅读:
    课程个人总结
    构建之法阅读笔记06
    构建之法读后感5
    第五周进度条
    提高自身能力
    活动图与状态机图
    对分析业务模型----类图的学习与认识
    需求分析工作的基本道理
    问题账户需求分析
    2016秋季个人阅读计划
  • 原文地址:https://www.cnblogs.com/micro-chen/p/9492312.html
Copyright © 2020-2023  润新知