DECLARE @ AS TABLE (t NVARCHAR(MAX)); INSERT INTO @(t) ...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @ AS  TABLE (t NVARCHAR(MAX));

INSERT INTO @(t)
VALUES ('The man walked down the road.'), ('Slowly, he ate the pie, savoring each delicious bite. He felt like he was truly happy.'),
       ('This sentence has no most frequent word.'), ('"That''s... that''s... that is just terrible!" he said. '), ('The old-fashioned man ate an old-fashioned cake.'), 
       ('IPv6 looks great, much better than IPv4, except for the fact that IPv6 has longer addresses.'), ('a   a            a b b b c');



SELECT o.t,IIF(COUNT(c)=1,MAX(value),'')
FROM @ o
LEFT JOIN (SELECT TOP 1 WITH TIES *
           FROM(SELECT t,value,COUNT(*)c
           FROM @ OUTER APPLY STRING_SPLIT(REPLACE(REPLACE(REPLACE(t,'"',''), ',',''),'.',''),' ')
                WHERE value<>''
                GROUP BY t,value)s
            ORDER BY RANK() OVER(PARTITION BY t ORDER BY c DESC)
)z ON o.t=z.t 
GROUP BY o.t

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results