大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索
打印 上一主题 下一主题

SQL Server列相加合并,字串相加 - Rowset string concatenation

[复制链接]
跳转到指定楼层
1#
发表于 2010-7-6 15:15:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
复制范例代码,SQL指令执行即可。
Copy and paste to SQL command and execute.
范例一 Sample 1
  1. -- Prepare sample data

  2. DECLARE        @Sample TABLE (ID INT, Code VARCHAR(3))

  3. INSERT        @Sample

  4. SELECT        290780, 'LT' UNION ALL

  5. SELECT        290780, 'AY' UNION ALL

  6. SELECT        290781, 'ILS' UNION ALL

  7. SELECT        290780, 'AY'

  8. -- Show the expected output

  9. SELECT DISTINCT        s1.ID,

  10. STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODES

  11. FROM         @Sample AS s1

  12. ORDER BY        s1.ID

  13. SELECT DISTINCT        s1.ID,

  14. STUFF((SELECT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODES

  15. FROM         @Sample AS s1

  16. ORDER BY        s1.ID

  17. SELECT DISTINCT        s1.ID,

  18. STUFF((SELECT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID FOR XML PATH('')), 1, 1, '') AS CODES

  19. FROM         @Sample AS s1

  20. ORDER BY        s1.ID
复制代码
范例二 Sample 2
  1. --1. 创建表,添加测试数据
  2. CREATE TABLE tb(id int, [value] varchar(10))
  3. INSERT tb SELECT 1, 'aa'
  4. UNION ALL SELECT 1, 'bb'
  5. UNION ALL SELECT 2, 'aaa'
  6. UNION ALL SELECT 2, 'bbb'
  7. UNION ALL SELECT 2, 'ccc'

  8. --SELECT * FROM tb
  9. /**//*
  10. id          value
  11. ----------- ----------
  12. 1           aa
  13. 1           bb
  14. 2           aaa
  15. 2           bbb
  16. 2           ccc

  17. (5 row(s) affected)
  18. */


  19. --2 在SQL2000只能用自定义函数实现
  20. ----2.1 创建合并函数fn_strSum,根据id合并value值
  21. GO
  22. CREATE FUNCTION dbo.fn_strSum(@id int)
  23. RETURNS varchar(8000)
  24. AS
  25. BEGIN
  26.     DECLARE @values varchar(8000)
  27.     SET @values = ''
  28.     SELECT @values = @values + ',' + value FROM tb WHERE id=@id
  29.     RETURN STUFF(@values, 1, 1, '')
  30. END
  31. GO

  32. -- 调用函数
  33. SELECT id, VALUE = dbo.fn_strSum(id) FROM tb GROUP BY id
  34. DROP FUNCTION dbo.fn_strSum

  35. ----2.2 创建合并函数fn_strSum2,根据id合并value值
  36. GO
  37. CREATE FUNCTION dbo.fn_strSum2(@id int)
  38. RETURNS varchar(8000)
  39. AS
  40. BEGIN
  41.     DECLARE @values varchar(8000)   
  42.     SELECT @values = isnull(@values + ',', '') + value FROM tb WHERE id=@id
  43.     RETURN @values
  44. END
  45. GO

  46. -- 调用函数
  47. SELECT id, VALUE = dbo.fn_strSum2(id) FROM tb GROUP BY id
  48. DROP FUNCTION dbo.fn_strSum2


  49. --3 在SQL2005中的新解法
  50. ----3.1 使用OUTER APPLY
  51. SELECT *
  52. FROM (SELECT DISTINCT id FROM tb) A OUTER APPLY(
  53.         SELECT [values]= STUFF(REPLACE(REPLACE(
  54.             (
  55.                 SELECT value FROM tb N
  56.                 WHERE id = A.id
  57.                 FOR XML AUTO
  58.             ), '<N value="', ','), '"/>', ''), 1, 1, '')
  59. )N

  60. ----3.2 使用XML
  61. SELECT id, [values]=STUFF((SELECT ','+[value] FROM tb t WHERE id=tb.id FOR XML PATH('')), 1, 1, '')
  62. FROM tb
  63. GROUP BY id

  64. --4 删除测试表tb
  65. drop table tb

  66. /**//*
  67. id          values
  68. ----------- --------------------
  69. 1           aa,bb
  70. 2           aaa,bbb,ccc

  71. (2 row(s) affected)
  72. */
复制代码

手机版|大马资讯论坛  

GMT+8, 2024-12-18 00:27 , Processed in 0.016360 second(s), 11 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表