id col
1 a
1 b
2 c
2 d
结果
1 a/b
2 c/d
id col col2
1 a a1
1 b b1
2 c c1
2 d d1
结果
1 a-a1/b-b1
2 c-c1/d-d1
这两种要怎么查询。不用函数.一条SQL语句。。高手帮忙
--原始表#temp
id NAME
1 a
1 b
2 C
2 D
select id,
stuff((select '/'+ltrim(name) from #temp where id=t.id for xml path('')),1,1,'') col
from #temp t
group by id
--原始表#temp2
id col1 col2
1 A A1
1 B B1
2 c c1
2 d d1
;WITH X AS(
SELECT ID,col1+'-'+col2 AS col FROM #temp2
)
select id,
stuff((select '/'+ltrim(col) from X where id=t.id for xml path('')),1,1,'') col1
from X t
group by id