拆分功能
CREATE FUNCTION [dbo].[udf_Split]
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
您的數據
DECLARE @TABLE TABLE (category INT,phrase VARCHAR(50))
INSERT INTO @TABLE VALUES
(0,'hello bye'),(0,'hello no'),(1,'hello bye'),
(1,'yes no'),(2,'hello bye')
查詢
SELECT category
, Data AS keyword
, COUNT(Data) AS frequency
FROM @TABLE t CROSS APPLY
(SELECT * FROM [dbo].[udf_Split](t.phrase, ' '))C
GROUP BY category,Data
ORDER BY category
結果集
╔══════════╦═════════╦═══════════╗
║ category ║ keyword ║ frequency ║
╠══════════╬═════════╬═══════════╣
║ 0 ║ bye ║ 1 ║
║ 0 ║ hello ║ 2 ║
║ 0 ║ no ║ 1 ║
║ 1 ║ bye ║ 1 ║
║ 1 ║ hello ║ 1 ║
║ 1 ║ no ║ 1 ║
║ 1 ║ yes ║ 1 ║
║ 2 ║ bye ║ 1 ║
║ 2 ║ hello ║ 1 ║
╚══════════╩═════════╩═══════════╝
SqlFiddle Demo
你有關鍵字的表格中的列表? –
@GordonLinoff不,我需要生成它 – arman