zoukankan      html  css  js  c++  java
  • SQL STUFF函数 同一列值拼接 拼接字符串 java程序员

    今日看到一篇文章,是关于和并列的,也研究了下,还是不错的

     

     

    要这种效果。

     

    create table tb(idintvalue varchar(10))

    insert into tbvalues(1,'aa')

    insert into tbvalues(1,'bb')

    insert into tbvalues(2,'aaa')

    insert into tbvalues(2,'bbb')

    insert into tbvalues(2,'ccc')

    go

     

    /*         stuff(param1, startIndex, length, param2)
    说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用param2替换删掉的字符。*/

    SELECT id, 
                          value = stuff
                              ((SELECT     ',' + value
                                  FROM         tb AS t
                                  WHERE     t .id = tb.id FOR xml path('')), 1, 1, '')
    FROM         tb
    GROUP BY id

     

    这样即可。

     

     

    ===================================================================================

    收集的资料

    1. /*  
    2. 标题:按某字段合并字符串之一(简单合并)  
    3. 作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  
    4. 时间:2008-11-06  
    5. 地点:广东深圳  
    6.   
    7. 描述:将如下形式的数据按id字段合并value字段。  
    8. id    value  
    9. ----- ------  
    10. 1     aa  
    11. 1     bb  
    12. 2     aaa  
    13. 2     bbb  
    14. 2     ccc  
    15. 需要得到结果:  
    16. id     value  
    17. ------ -----------  
    18. 1      aa,bb  
    19. 2      aaa,bbb,ccc  
    20. 即:group by id, 求 value 的和(字符串相加)  
    21. */  
    22. --1、sql2000中只能用自定义的函数解决  
    23. create table tb(id int, value varchar(10))  
    24. insert into tb values(1, 'aa')  
    25. insert into tb values(1, 'bb')  
    26. insert into tb values(2, 'aaa')  
    27. insert into tb values(2, 'bbb')  
    28. insert into tb values(2, 'ccc')  
    29. go  
    30.   
    31. create function dbo.f_str(@id varchar(10)) returns varchar(1000)  
    32. as  
    33. begin  
    34.   declare @str varchar(1000)  
    35.   select @str = isnull(@str + ',' , '') + cast(value as varcharfrom tb where id = @id  
    36.   return @str  
    37. end  
    38. go  
    39.   
    40. --调用函数  
    41. select id , value = dbo.f_str(id) from tb group by id  
    42.   
    43. drop function dbo.f_str  
    44. drop table tb  
    45.   
    46.   
    47. --2、sql2005中的方法  
    48. create table tb(id int, value varchar(10))  
    49. insert into tb values(1, 'aa')  
    50. insert into tb values(1, 'bb')  
    51. insert into tb values(2, 'aaa')  
    52. insert into tb values(2, 'bbb')  
    53. insert into tb values(2, 'ccc')  
    54. go  
    55.   
    56. select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')  
    57. from tb  
    58. group by id  
    59.   
    60. drop table tb  
    61.   
    62.   
    63. --3、使用游标合并数据  
    64. create table tb(id int, value varchar(10))  
    65. insert into tb values(1, 'aa')  
    66. insert into tb values(1, 'bb')  
    67. insert into tb values(2, 'aaa')  
    68. insert into tb values(2, 'bbb')  
    69. insert into tb values(2, 'ccc')  
    70. go  
    71. declare @t table(id int,value varchar(100))--定义结果集表变量  
    72. --定义游标并进行合并处理  
    73. declare my_cursor cursor local for  
    74. select id , value from tb  
    75. declare @id_old int , @id int , @value varchar(10) , @s varchar(100)  
    76. open my_cursor  
    77. fetch my_cursor into @id , @value  
    78. select @id_old = @id , @s=''  
    79. while @@FETCH_STATUS = 0  
    80. begin  
    81.     if @id = @id_old  
    82.        select @s = @s + ',' + cast(@value as varchar)  
    83.     else  
    84.       begin  
    85.         insert @t values(@id_old , stuff(@s,1,1,''))  
    86.         select @s = ',' + cast(@value as varchar) , @id_old = @id  
    87.       end  
    88.     fetch my_cursor into @id , @value  
    89. END  
    90. insert @t values(@id_old , stuff(@s,1,1,''))  
    91. close my_cursor  
    92. deallocate my_cursor  
    93.   
    94. select * from @t  
    95. drop table tb  
  • 相关阅读:
    warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    Windows10+CLion+OpenCV4.5.2开发环境搭建
    Android解决部分机型WebView播放视频全屏按钮灰色无法点击、点击全屏白屏无法播放等问题
    MediaCodec.configure Picture Width(1080) or Height(2163) invalid, should N*2
    tesseract
    Caer -- a friendly API wrapper for OpenCV
    Integrating OpenCV python tool into one SKlearn MNIST example for supporting prediction
    Integrating Hub with one sklearn mnist example
    What is WSGI (Web Server Gateway Interface)?
    Hub --- 机器学习燃料(数据)的仓库
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215420.html
Copyright © 2011-2022 走看看