Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how you can do it, first you have to create a function that splits strings, afterwards count the occurrences and display them as you want. It makes it more complicated because you want to display variable number of columns:</p> <pre><code>/* CREATE FUNCTION dbo.SplitStrings_XML ( @List NVARCHAR(MAX), @Delimiter NVARCHAR(255) ) RETURNS TABLE WITH SCHEMABINDING AS RETURN ( SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)') FROM ( SELECT x = CONVERT(XML, '&lt;i&gt;' + REPLACE(@List, @Delimiter, '&lt;/i&gt;&lt;i&gt;') + '&lt;/i&gt;').query('.') ) AS a CROSS APPLY x.nodes('i') AS y(i) ); GO */ CREATE TABLE #t(ID INT, Col VARCHAR(1000)) INSERT #t VALUES (1, 'This is a Sample Text'), (2, 'Sample Text is typed here'), (3, 'Here the sample text is'), (4, 'Typing a sample') DECLARE @MinimumNumberOfOccurances INT = 2 SELECT a.ID, a.Col, b.Item INTO #SplitedStrings FROM #t a CROSS APPLY dbo.SplitStrings_XML(a.Col, N' ') b SELECT b.Item, COUNT(*) cnt INTO #SplitedStringsGrouped FROM #t a CROSS APPLY dbo.SplitStrings_XML(a.Col, N' ') b GROUP BY b.Item SELECT b.*, a.cnt INTO #ResultTable FROM #SplitedStringsGrouped a RIGHT JOIN #SplitedStrings b ON b.Item = a.Item AND a.cnt &gt; @MinimumNumberOfOccurances ORDER BY b.ID, a.cnt DESC, LEN(a.Item) DESC DECLARE @ColumnNames VARCHAR(1000) = STUFF( ( SELECT ',[' + Item + ']' FROM #SplitedStringsGrouped WHERE cnt &gt; @MinimumNumberOfOccurances FOR XML PATH('') ) , 1, 1, '') DECLARE @TableHeader VARCHAR(1000) = STUFF( ( SELECT ',MAX([' + Item + ']) AS [Common' + CAST((ROW_NUMBER() OVER (ORDER BY cnt DESC, LEN(Item) DESC) - 1) AS VARCHAR(5)) + ']' FROM #SplitedStringsGrouped WHERE cnt &gt; @MinimumNumberOfOccurances FOR XML PATH('') ) , 1, 1, '') SELECT ID, Item, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) Num INTO #NonCommon FROM #ResultTable WHERE cnt IS NULL DECLARE @sql VARCHAR(1000) = ' SELECT MAX(pvt.ID) ID, MAX(pvt.Col) [Column], '+@TableHeader+', RTRIM(( SELECT a.Item + '' '' FROM #NonCommon a WHERE a.ID = pvt.ID FOR XML PATH('''') )) NonCommon FROM #ResultTable a PIVOT ( MAX(Item) FOR Item IN ('+@ColumnNames+') ) pvt GROUP BY pvt.ID ' EXEC(@sql) DROP TABLE #t DROP TABLE #SplitedStringsGrouped DROP TABLE #SplitedStrings DROP TABLE #ResultTable DROP TABLE #NonCommon </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload