Friday, March 9, 2012

HELP with query

I have the following records:
Date EmployeeId Project
20050503 12345 VERIZON
20050503 12345 CINGULAR
20050503 12345 SPRINT
20050503 24680 CINGULAR
I need the resulting table to look like this:
Date EmployeeId Project
20050503 12345 VERIZON, CINGULAR, SPRINT
20050503 24680 CINGULAR
How do I separate the projects with a comma?
Thanks,
NinelHi
The best place to do this is on the client, but these may help
http://tinyurl.com/6rhsj
John
"ninel" wrote:

> I have the following records:
> Date EmployeeId Project
> 20050503 12345 VERIZON
> 20050503 12345 CINGULAR
> 20050503 12345 SPRINT
> 20050503 24680 CINGULAR
> I need the resulting table to look like this:
> Date EmployeeId Project
> 20050503 12345 VERIZON, CINGULAR, SPRINT
> 20050503 24680 CINGULAR
> How do I separate the projects with a comma?
> Thanks,
> Ninel
>|||create table _data
(
Date datetime,
EmployeeId int,
Project varchar(50)
)
insert into _data values('20050503', 12345, 'VERIZON')
insert into _data values('20050503', 12345, 'CINGULAR')
insert into _data values('20050503', 12345, 'SPRINT')
insert into _data values('20050503', 24680, 'CINGULAR')
create function dbo.Projects
(
@.intEmployeeId int
)
returns varchar(8000)
as
begin
declare @.vcProjects varchar(8000)
set @.vcProjects = ''
select
@.vcProjects = @.vcProjects + case when @.vcProjects = '' then '' else ', '
end + Project
from
_data (nolock)
where
EmployeeId = @.intEmployeeId
return @.vcProjects
end
select
Date, EmployeeId, dbo.Projects(EmployeeId)
from
_data (nolock)
group by
Date, EmployeeId
Hope this works.
Best Regards,
Lakshman.
"ninel" wrote:

> I have the following records:
> Date EmployeeId Project
> 20050503 12345 VERIZON
> 20050503 12345 CINGULAR
> 20050503 12345 SPRINT
> 20050503 24680 CINGULAR
> I need the resulting table to look like this:
> Date EmployeeId Project
> 20050503 12345 VERIZON, CINGULAR, SPRINT
> 20050503 24680 CINGULAR
> How do I separate the projects with a comma?
> Thanks,
> Ninel
>

No comments:

Post a Comment