Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have data in a table emp as

UserName Department
Mohd.wasif Web
Mohd.wasif Sigma
mohd.wasif SQL Server
Sumit.singh C#
Sumit.singh Oracle
Sumit.singh Java

Now I I select UserName mohd.wasif then output should be

SQL
UserName      Department 
mohd.wasif   Web,Sigma,SQL Server


there after I want to join the output from this query with another table as Register having column UserName .

Please Help
Posted
Updated 25-Mar-12 19:58pm
v3

Hi Mohd Wasif,


Try the following


SQL
SELECT username as Name,
           MAX( CASE seq WHEN 1 THEN department ELSE '' END ) + ', ' +
           MAX( CASE seq WHEN 2 THEN department ELSE '' END ) + ', ' +
           MAX( CASE seq WHEN 3 THEN department ELSE '' END ) + ', ' +
           MAX( CASE seq WHEN 4 THEN department ELSE '' END ) as Department
      FROM ( SELECT p1.username, p1.department ,
                    ( SELECT COUNT(*)
                        FROM emp p2
                        WHERE p2.username = p1.username
                        AND p2.department <= p1.department )
             FROM emp p1 ) D ( username, department, seq )
     where username='Mohd.wasif' GROUP BY username ;



Thanks
 
Share this answer
 
 
Share this answer
 
Please try this if you are planning to have as s stored proc.


SQL
BEGIN
DECLARE @DepartmentNames VARCHAR(1000)

DECLARE @UserName AS VARCHAR(20)

SET @UserName = 'Mohd.wasif'

    SELECT @DepartmentNames = COALESCE(@DepartmentNames,'') + Department+ ','
    FROM Emp WHERE UserName = @UserName

SELECT A.* FROM
    (SELECT @UserName AS UserName, @DepartmentNames AS Dept) AS A
--INNER JOIN Register AS B
--ON A.UserName = B.UserName

END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900