合并分拆表数据

  原贴地址: http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
  1. /******************************************************************************************************************************************************
  2. 合并分拆表数据
  3. 整理人:中国风(Roy)
  4. 日期:2008.06.06
  5. ******************************************************************************************************************************************************/
  6. --> --> (Roy)生成測試數據
  7. if not object_id('Tab'is null
  8.     drop table Tab
  9. Go
  10. Create table Tab([Col1] int,[Col2] nvarchar(1))
  11. Insert Tab
  12. select 1,N'a' union all
  13. select 1,N'b' union all
  14. select 1,N'c' union all
  15. select 2,N'd' union all
  16. select 2,N'e' union all
  17. select 3,N'f'
  18. Go
  19. 合并表:
  20. SQL2000用函数:
  21. go
  22. if object_id('F_Str'is not null
  23.     drop function F_Str
  24. go
  25. create function F_Str(@Col1 int)
  26. returns nvarchar(100)
  27. as
  28. begin
  29.     declare @S nvarchar(100)
  30.     select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1
  31.     return @S
  32. end
  33. go
  34. Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab
  35. go
  36. SQL2005用XML:
  37. 方法1:
  38. select 
  39.     a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'')
  40. from 
  41.     (select distinct COl1 from Tab) a
  42. Cross apply
  43.     (select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b
  44. 方法2:
  45. select 
  46.     a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44))
  47. from 
  48.     (select distinct COl1 from Tab) a
  49. cross apply
  50.     (select Col2=(select COl2 from Tab  where COl1=a.COl1 FOR XML AUTO, TYPE)
  51.                 .query('<Tab>
  52.                 {for $i in /Tab[position()<last()]/@COl2 return concat(string($i),",")}
  53.                 {concat("",string(/Tab[last()]/@COl2))}
  54.                 </Tab>')
  55.                 )b
  56. SQL05用CTE:
  57. ;with roy as(select Col1,Col2,row=row_number()over(partition by COl1 order by COl1) from Tab)
  58. ,Roy2 as
  59. (select COl1,cast(COl2 as nvarchar(100))COl2,row from Roy where row=1 
  60. union all 
  61. select a.Col1,cast(b.COl2+','+a.COl2 as nvarchar(100)),a.row from Roy a join Roy2 b on a.COl1=b.COl1 and a.row=b.row+1)
  62. select Col1,Col2 from Roy2 a where row=(select max(row) from roy where Col1=a.COl1) order by Col1 option (MAXRECURSION 0)
  63. 生成结果:
  64. /*
  65. Col1        COl2
  66. ----------- ------------
  67. 1           a,b,c
  68. 2           d,e
  69. 3           f
  70. (3 行受影响)
  71. */
  72. 拆分表:
  73. --> --> (Roy)生成測試數據
  74. if not object_id('Tab'is null
  75.     drop table Tab
  76. Go
  77. Create table Tab([Col1] int,[COl2] nvarchar(5))
  78. Insert Tab
  79. select 1,N'a,b,c' union all
  80. select 2,N'd,e' union all
  81. select 3,N'f'
  82. Go
  83. SQL2000用辅助表:
  84. if object_id('Tempdb..#Num'is not null
  85.     drop table #Num
  86. go
  87. select top 100 ID=Identity(int,1,1) into #Num from syscolumns a,syscolumns b
  88. Select 
  89.     a.Col1,COl2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) 
  90. from 
  91.     Tab a,#Num b
  92. where
  93.     charindex(',',','+a.Col2,b.ID)=b.ID --也可用 substring(','+a.COl2,b.ID,1)=','
  94. SQL2005用Xml:
  95. select 
  96.     a.COl1,b.Col2
  97. from 
  98.     (select Col1,COl2=convert(xml,'<root><v>'+replace(COl2,',','</v><v>')+'</v></root>') from Tab)a
  99. outer apply
  100.     (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/root/v')C(v))b
  101. SQL05用CTE:
  102. ;with roy as 
  103. (select Col1,COl2=cast(left(Col2,charindex(',',Col2+',')-1) as nvarchar(100)),Split=cast(stuff(COl2+',',1,charindex(',',Col2+','),''as nvarchar(100)) from Tab
  104. union all
  105. select Col1,COl2=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),''as nvarchar(100)) from Roy where split>''
  106. )
  107. select COl1,COl2 from roy order by COl1 option (MAXRECURSION 0)
  108. 生成结果:
  109. /*
  110. Col1        COl2
  111. ----------- -----
  112. 1           a
  113. 1           b
  114. 1           c
  115. 2           d
  116. 2           e
  117. 3           f
  118. */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值