これを行う方法は次のとおりです。最初に文字列を分割する関数を作成し、その後、出現回数を数えて、必要に応じて表示する必要があります。可変数の列を表示するため、より複雑になります。
/*
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, '<i>' +
REPLACE(@List, @Delimiter, '</i><i>') + '</i>').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 > @MinimumNumberOfOccurances
ORDER BY b.ID, a.cnt DESC, LEN(a.Item) DESC
DECLARE @ColumnNames VARCHAR(1000) = STUFF(
(
SELECT ',[' + Item + ']'
FROM #SplitedStringsGrouped
WHERE cnt > @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 > @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