SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起。

后在网上查找,找到了可以实现此功能的方法,特此记录下。

SELECT
	a,
	stuff((SELECT ',' + b FROM #tb WHERE a = t.a FOR xml path('')),
		1,
		1,
		''
	)AS b from  # tb AS t
GROUP BY
	a;

先对a列进行分组,对分组中的b以Xml形式输出,再使用stuff将开关多出的,删掉。

 

具体实现参考:http://blog.itnmg.net/sqlserver-group_concat/

对于其实使用到的for xml path 参考 http://www.cnblogs.com/doubleliang/archive/2011/07/06/2098775.html

stuff 参考 http://msdn.microsoft.com/zh-cn/library/ms188043.aspx

相关文章:

  • 2021-12-28
  • 2021-09-21
  • 2021-11-07
  • 2022-12-23
  • 2021-11-07
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案