• 字符串处理の合并记录行


    今天在群里看到有朋友问,怎么合并记录行呢?想想自己以前工作中有做过这些,贴出来,备忘吧

    上面的图片就是原始数据和我们最终想要的结果,怎么来做呢?直接能想到的有2种方式:写函数OR使用SQL For Xml,写函数的方式没什么好写的,declare一个变量就可以搞定,SQL For Xml的语法有点记不大清楚【平时很少写SQL了】好在有联机帮助文档,下面是完整的代码:

    declare @T table(tpe int,titles varchar(50))
    insert into @T select 1,'A' 
    union all select 1,'b'
    union all select 2,'A'
    union all select 2,'B'
    union all select 2,'C'
    union all select 2,'D'
    
    select * from @T
    
    select tpe, Tvalues=STUFF((SELECT '|' +CAST(titles AS varchar) FROM @T where tpe=a.tpe FOR XML PATH('')),1,1,'') 
    from @t a group by tpe 
     


     

  • 相关阅读:
    Java异常处理和设计
    一次qps测试实践
    Alternate Task UVA
    Just Another Problem UVA
    Lattice Point or Not UVA
    Play with Floor and Ceil UVA
    Exploring Pyramids UVALive
    Cheerleaders UVA
    Triangle Counting UVA
    Square Numbers UVA
  • 原文地址:https://www.cnblogs.com/mfkaudx/p/3579919.html
Copyright © 2020-2023  润新知