split comma seprate value from table in sql server

前端 未结 3 1042
迷失自我
迷失自我 2021-01-25 10:21

i have one SQL table in which many records, i want to know how many names are in it and how much time one name in it.

Table NameMst

Name

jo         


        
3条回答
  •  长情又很酷
    2021-01-25 10:56

    SELECT y.Name, count(*) Count
    FROM
    (VALUES
    ('john,smith,alax,rock'),
    ('smith,alax,sira'),
    ('john,rock'),
    ('rock,sira')) x(names)
    CROSS APPLY
    (
    SELECT t.c.value('.', 'VARCHAR(2000)') Name
         FROM (
             SELECT x = CAST('' + 
                   REPLACE(x.names, ',', '') + '' AS XML)
         ) a
         CROSS APPLY x.nodes('/t') t(c)
         ) y
    GROUP BY y.Name
    

    Result:

    Name   Count
    alax   2
    john   2
    rock   3
    sira   2
    smith  2
    

提交回复
热议问题