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
 SQL Server Development (2000)
 Query help in grouping

Author  Topic 

thanksfor help
Posting Yak Master

106 Posts

Posted - 2009-03-16 : 13:53:01
Hi,

I have table

emp project
ABCD P1
ABCD P2
ABCD P3
PQR P2
PQR P4
XYZ P3
XYZ P4

I need help in writing a query to format,

emp PROJECT
ABCD P1,P2,P3
PQR P2,P4
XYZ P3,P4

Any help very much appreciated

thanks in advance

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-16 : 13:56:57
you need to create a UDF like this

CREATE FUNCTION GetProjects
(
@Emp varchar(100)
)
RETURNS Varchar(8000)
AS
BEGIN
DECLARE @Ret varchar(8000)
SELECT @Ret=COALESCE(@Ret+',','') + PROJECT
FROM Table
WHERE emp=@Emp
RETURN @Ret
END

then use it like below

SELECT DISTINCT emp,dbo.GetProjects(emp)
FROM Table
Go to Top of Page
   

- Advertisement -