晚飯過后,打開QQ圈子,發現QQ群里有人提問了一個問題↓
數據表中有這樣的數據 1 100 1 101 1 106 2 100 2 109 3 112 如何轉換為 1 100,101,106 2 100,109 3 112
知道寫存儲過程或者函數可以解決,但是想想能不能用一條sql語句解決...未果...
還是去搜索了下怎么搞,→轉載鏈接←
create table tb(id int, value varchar(10)) insert into tb values(1, 'aa') insert into tb values(1, 'bb') insert into tb values(2, 'aaa') insert into tb values(2, 'bbb') insert into tb values(2, 'ccc') go create function [dbo].[f_str](@id int) returns nvarchar(1000) as begin declare @str nvarchar(1000) set @str = '' select @str = @str + ',' + cast(value as nvarchar(900)) from tb where id = @id set @str = right(@str , len(@str) - 1) return @str end go --調用函數 select id , value = dbo.f_str(id) from tb group by id