晚上看到培的BLOG上的随笔,希望能用简单的方法实现。
uid rname
1 stefanie
1 google
1 sina
2 baidu
2 microsoft
---------------
--想得到的结果
1 stefanie,google,sina
2 baidu,microsoft
我的实现方法:
ALTER FUNCTION FUN_MER(@UID NVARCHAR (20))
RETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @TEMP NVARCHAR(1000)
SET @TEMP = ''
SELECT @TEMP = @TEMP + RNAME + ',' FROM PLS WHERE UID = @UID
RETURN STUFF(@TEMP,LEN(@TEMP),1,'')
END
SELECT distinct UID + ' ' + DBO.FUN_MER(UID) FROM PLSRETURNS NVARCHAR(200)
AS
BEGIN
DECLARE @TEMP NVARCHAR(1000)
SET @TEMP = ''
SELECT @TEMP = @TEMP + RNAME + ',' FROM PLS WHERE UID = @UID
RETURN STUFF(@TEMP,LEN(@TEMP),1,'')
END
结果:
1 stefanie, google, sina
2 baidu, mircsoft