Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Need help with the query

Author  Topic 

texdon21
Starting Member

1 Post

Posted - 2005-11-22 : 10:48:24
I have a table Employee with just one column Employee_Name.

Eg.

Employee_name

Victor Michael Peter
David Peter Andrew
Kris Victor
Mathew David
Andrew
Michael Mark Peter
……….

I want the result as

(Name ) (Count)
Victor 2
Michael 2
Peter 3
David 2
Andrew 2
Kris 1
Mathew 1
Mark 1

Can somebody please tell me how to write a query for this.

Thanks,
Mike

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-11-22 : 11:35:28
Could use parsename to break them out:


ref: [url]http://www.sqlteam.com/item.asp?ItemID=15044[/url]


declare @test table (employee_name  varchar(100))
insert into @test
select 'Victor Michael Peter' union
select 'David Peter Andrew' union
select 'Kris Victor' union
select 'Mathew David' union
select 'Andrew' union
select 'Michael Mark Peter'


-- update spaces with periods
update @test
set employee_name = replace(replace(employee_name, ' ', '.'),' ', '.')

select d.sub_name 'name', count(*) 'count'
from @test t
inner join (
select parsename(employee_name,1) 'sub_name'
from @test
union
select parsename(employee_name,2)
from @test
union
select parsename(employee_name,3)
from @test) d
on t.employee_name like '%' + d.sub_name + '%'
where d.sub_name is not null
group by d.sub_name


Nathan Skerl
Go to Top of Page

ksandeep
Starting Member

10 Posts

Posted - 2005-11-23 : 06:38:38
can i give u a procedure, will that do ?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-23 : 08:11:34
quote:
Originally posted by ksandeep

can i give u a procedure, will that do ?



What is that procedure?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -