Jack
Jack

Reputation: 7557

Tricky SQL query requiring search for contains

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.

Upvotes: 2

Views: 433

Answers (5)

dilip kumar
dilip kumar

Reputation: 517

  
    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

Upvotes: 0

Mait Heel
Mait Heel

Reputation: 1

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"

Upvotes: 0

Sankara
Sankara

Reputation: 1479

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

Upvotes: 1

Rikki
Rikki

Reputation: 3528

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

Upvotes: 1

podiluska
podiluska

Reputation: 51494

;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)))

Upvotes: 1

Related Questions