2

I have data such as this:

Inventors column in my table

Hundley; Edward; Ana
Isler; Hunsberger
Hunsberger;Hundley

Names are separated by ;. I want to write a SQL query which sums up the count.

Eg. The result should be:

Hundley     2
Isler       1
Hunsberger  2
Edward      1
Ana         1

I could do a group by but this is not a simple group by as you can see. Any ideas/thoughts on how to get this output?

Edit: Changed results so it doesn't create any confusion that a row only contains 2 names.

4

5 に答える 5

1

You can take a look at this. I certainly do not recommend this way if you have lots of data, BUT you can do some modifications and use it and it works like a charm!

This is the new code for supporting unlimited splits:

Declare @Table  Table   (
    Name    Nvarchar(50)
);

Insert  @Table  (
    Name
)   Select  'Hundley; Edward; Anna'
Union   Select  'Isler; Hunsberger'
Union   Select  'Hunsberger; Hundley'
Union   Select  'Anna'
;

With    Result  (
    Part
,   Remained
,   [Index]
,   Level
)   As  (
    Select  Case    When    CharIndex(';', Name, 1) =   0
            Then    Name
            Else    Left(Name, CharIndex(';', Name, 1) - 1)
        End
    ,   Right(Name, Len(Name) - CharIndex(';', Name, 1))
    ,   CharIndex(';', Name, 1)
    ,   1
        From    @Table
Union   All
    Select  LTrim(
        Case    When    CharIndex(';', Remained, 1) =   0
            Then    Remained
            Else    Left(Remained, CharIndex(';', Remained, 1) - 1)
        End
    )
    ,   Right(Remained, Len(Remained) - CharIndex(';', Remained, 1))
    ,   CharIndex(';', Remained, 1)
    ,   Level
    +   1
        From    Result
        Where   [Index] <>  0
)   Select  Part
    ,   Count(*)
        From    Result
        Group   By  Part

Cheers

于 2012-09-08T06:02:51.207 に答える
1
;with cte as  
( 
    select 1 as Item, 1 as Start, CHARINDEX(';',inventors, 1) as Split, Inventors from YourInventorsTable
    union all 
    select cte.Item+1, cte.Split+1, nullif(CHARINDEX(';',inventors, cte.Split+1),0), inventors as Split 
    from cte 
    where cte.Split<>0   
)    

select rTRIM(lTRIM(SUBSTRING(inventors, start,isnull(split,len(inventors)+1)-start))), count(*)
from cte  
group by rTRIM(lTRIM(SUBSTRING(inventors, start,isnull(split,len(inventors)+1)-start)))
于 2012-09-08T06:57:03.770 に答える
1

You can create a split function to split the col values

select splittedValues.items,count(splittedValues) from table1
cross apply dbo.split(col1,';') splittedValues
group by splittedValues.items

DEMO in Sql fiddle

于 2012-09-08T21:56:39.337 に答える
0

first make one function who take your comma or any other operator(;) separated string into one table and by using that temp table, apply GROUP function on that table.

So you will get count for separate value.

"select d.number,count(*) from (select number from dbo.CommaseparedListToTable('Hundley;Edward;Ana;Isler;Hunsberger;Hunsberger;Hundley',';'))d group by d.number"

于 2013-12-06T10:57:33.207 に答える
0
  
    declare @text nvarchar(255) = 'Edward; Hundley; AnaIsler; Hunsberger; Hunsberger; Hundley ';
    declare @table table(id int identity,name varchar(50));

    while @text like '%;%'
    Begin
    insert into @table (name)
    select SUBSTRING(@text,1,charindex(';',@text)-1)
    set @text = SUBSTRING(@text, charindex(';',@text)+1,LEN(@text))
    end 
    insert into @table (name)
    select @text
    select name , count(name ) counts from @table group by name

Output

name      count 
AnaIsler    1
Hundley     2
Hunsberger  2
Edward      1
于 2015-03-26T11:03:42.990 に答える