Sql server function for displaying word frequency in a column

后端 未结 3 1119
醉酒成梦
醉酒成梦 2020-12-06 08:18

I have a table that lists a freet text input from a survey where enterents were allowed to enter their responses (regarding colours they would like to have in their wedding)

3条回答
  •  执笔经年
    2020-12-06 08:57

    DECLARE @phrases TABLE (id int, phrase varchar(max))
    INSERT @phrases values
    (1,'Red and White'  ),
    (2,'green'          ),
    (3,'White and blue' ),
    (4,'Blue'           ),
    (5,'Dark blue'      );
    
    SELECT word, COUNT(*) c
    FROM @phrases
    CROSS APPLY (SELECT CAST(''+REPLACE(phrase,' ','')+'' AS xml) xml1 ) t1
    CROSS APPLY (SELECT n.value('.','varchar(max)') AS word FROM xml1.nodes('a') x(n) ) t2
    GROUP BY word
    
    word         freq
    ----------- -----------
    and         2
    blue        3
    Dark        1
    green       1
    Red         1
    White       2
    

提交回复
热议问题