问题描述
解决方案
这个是要问什么问题?
解决方案二:
使用case when,行转列,
http://www.jb51.net/article/51167.htm
http://blog.csdn.net/wuxiaokaixinguo/article/details/8523048
解决方案三:
直接写报表,然后分组就可以了
解决方案四:
我发你个自己写的函数:
/******************************************************
- SQLServer字符串批量拆分函数
--原始数据
id names
----------- --------------------
1 ljy,zwg,cym
2 czg,zy
--生成的数据
id rn name
----------- ----------- ----------
1 1 ljy
1 2 zwg
1 3 cym
2 1 czg
2 2 zy
--示例1:
declare @table table(id int , names varchar(20))
insert into @table select 1, 'ljy,zwg,cym'
insert into @table select 2 , 'czg,zy'
SELECT * FROM @table
SELECT a.id,rn,b.v AS name
FROM @table AS a outer apply dbo.uf_split_string_batch(a.names)b
--示例2:
SELECT a.c_guid,rn,b.v AS name
FROM tb_contract a
outer apply dbo.uf_split_string_batch(a.c_store_id)b
*************************************************************************/
CREATE FUNCTION uf_split_string_batch(@value varchar(MAX))
RETURNS @table TABLE(rn int,v varchar(MAX))
AS
BEGIN
INSERT INTO @table
SELECT b.*
FROM(SELECT CONVERT(XML , '' + REPLACE(@value, ',','' )+ '' ) AS f )a
OUTER APPLY
(
SELECT rn = row_number ()OVER (ORDER BY getdate ()),t.c.value ('.' , 'varchar(max)' ) AS f
FROM a.f.nodes ('//v' ) AS t(c )
)b
RETURN
END