SQL Server:打破逗号分隔的字符串并匹配表中的值

时间:2018-08-31 19:08:22

标签: sql sql-server

我正在使用SQL Server,并且有一个“标签”列,该列包含一串用逗号分隔的标签。有没有办法将字符串分解并放在另一个表中,并使它们匹配以便能够轻松查看喜欢的标签?

第1行:

3years_andmore,access_ccc,access_sdl,associate_iii,ccc_tickets,desoto_counter,phone_call__property_tax,ticketing,trainer

第2行:

3years_andmore,access_ccc,access_sdl,associate_iii,ccc_tickets,desoto_counter,phone_call__dmv,ticketing,trainer

第3行:

5_minutes,access_ccc,access_sdl,associate_ii,ccc_tickets,desoto_counter,lessthan_3years,phone_call__operations___title_by_mail_inquiry,trainer

第4行:

access_ccc,access_sdl,associate_ii,ccc_customer_request_manager_other,ccc_tickets,desoto_counter,lessthan_3years,phone_call__associate_requesting_manager__customer_requesting_mr_brierton,trainer

字段中没有真正的顺序,标签也不相同,但是有没有办法至少分类到一个新表中并将其分解并匹配,以查看哪些票证具有相同的标签?

SELECT 
    [id],
    [url],
    [external_id],
    [type],
    [subject],
    [description],
    [priority],
    [status],
    [recipient],
    [requester_id],
    [submitter_id],
    [assignee_id],
    [organization_id],
    [group_id],
    [collaborator_ids],
    [forum_topic_id],
    [problem_id],
    [has_incidents],
    [due_at],
    [tags],
    [via],
    [custom_fields],
    [satisfaction_rating],
    [sharing_agreement_ids],
    [followup_ids],
    [ticket_form_id],
    [created_at],
    [updated_at],
    [channel]
FROM 
    [Brierton].[dbo].[Tickets]
WHERE
    created_at BETWEEN '2017-11-01' AND '2018-08-23' 
    AND ',' + tags + ',' LIKE '%,' + 'ccc_tickets' + ',%'

1 个答案:

答案 0 :(得分:1)

自2016版以来,内置了string_split()函数。要以“更相关的方式”获取每张票证的标签,可以使用:

SELECT t.id,
       x.value
       FROM tickets t
            CROSS APPLY (SELECT value
                                FROM string_split(t.tags, ',')) x;

您可以从那里进行汇总以获得最常用的标签。

SELECT x.value,
       count(*)
       FROM tickets t
            CROSS APPLY (SELECT value
                                FROM string_split(t.tags, ',')) x
       GROUP BY x.value
       ORDER BY count(*) DESC;

db<>fiddle