I agree with jsmith. That is probably your best bet if you have the ability to create the user defined function.otherwise:declare @users table (userid int, username varchar(128))declare @UserOptions table (userid int, optionID int)insert into @users select 1, 'blastrix' unionselect 2, 'yakoo'insert into @UserOptionsselect 1, 1 unionselect 1, 2 unionselect 1, 3 unionselect 2, 1 unionselect 2, 3 unionselect 2, 5declare @workingtable table (userid int, options varchar(100))insert into @workingtable (userid, options)SELECT u.userid, uo.optionID FROM @users u INNER JOIN @UserOptions uo ON (u.userid = uo.userid)ORDER BY u.userid, uo.optioniddeclare @list varchar(8000), @last intselect @list = '', @last = -1update @workingtableset @list = options = case when @last <> userid then options else @list + ', ' + options end, @last = useridselect w1.userid, w1.options from @workingtable w1
you will then need to select the column with the greatest options lengthGot SQL?