Это буквально ', у которого самый длинный?' конкурс!
Вы можете попробовать следующее, что более справедливо, чем разбиение, так как он вернет всех кандидатов с максимальной длиной.
SELECT p2.id, p2.permissions
FROM table1 p2
JOIN (SELECT p.id, MAX(LEN(p.permissions)) AS longest FROM table1 p GROUP BY p.id)
p1 ON p1.id = p2.id
AND p1.longest = Len(p2.permissions)
Тест
--drop table if exists table1
create table table1 ( id int, permissions nvarchar(100));
insert into table1 values(1, 'Walk, Swim');
insert into table1 values(1, 'Walk, Sit, Swim, Run, Jump, Lay');
insert into table1 values(1, '!Walk, Sit, Lay');
insert into table1 values(2, 'Walk, Sit, Swim');
insert into table1 values(3, '!Walk, Sit, Swim');
insert into table1 values(3, 'Walk, Sit, Swim');
SELECT p2.id, p2.permissions
FROM table1 p2
JOIN (SELECT p.id, MAX(LEN(p.permissions)) AS longest FROM table1 p GROUP BY p.id)
p1 ON p1.id = p2.id
AND p1.longest = Len(p2.permissions)
Выход
id permissions
1 Walk, Sit, Swim, Run, Jump, Lay
2 Walk, Sit, Swim
3 !Walk, Sit, Swim
Несколько «длинных»
(...)
insert into table1 values(1, 'Walk, Swim');
insert into table1 values(1, 'Walk, Sit, Swim, Run, Jump, Lay');
insert into table1 values(1, '!Walk, Sit, Lay');
insert into table1 values(2, 'Walk, Sit, Swim');
insert into table1 values(3, '!Walk, Sit, Swim');
insert into table1 values(3, 'Walk, Sit, Swim');
insert into table1 values(3, '!Walk, Run, Swim'); -- extra record
Выход:
id permissions
1 Walk, Sit, Swim, Run, Jump, Lay
2 Walk, Sit, Swim
3 !Walk, Sit, Swim
3 !Walk, Run, Swim