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 2005 Forums
 Transact-SQL (2005)
 MS SQL query problem

Author  Topic 

crisgomez
Starting Member

3 Posts

Posted - 2011-01-10 : 04:04:49
Table User

Id Username
1 A
2 B
3 C
4 D

Table Roles

Id UserId Role Status Expiration
1 1 Admin Active 01-01-2011
2 2 Client Active 02-02-2011
3 3 Applicant Active 03-03-2011
4 4 Client Inactive 04-04-2011

Output:

A B C D -- this is the username
Admin Client Applicant Client -- role of the username
Active Active Active inactive -- status of the username
01-01-2011 02-02-2011 03-03-2011 04-04-2011 -- expiration of username

I want to achieved the result above. User and Roles table are having a one to one relationship. Username along with the roles information should be in one column respectively. For example User A having a roles information of Admin, Active and 01-01-2011 came from Role, Status, Expiration fields respectively of table Roles. any help pls on how to achieved this result?

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-01-10 : 04:52:54
You should look up the pivot statement: http://msdn.microsoft.com/en-us/library/ms177410(SQL.90).aspx

You will however encounter some datatype problems with your output requirements. You would probably need to convert them all to varchar to achieve what you want.

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page
   

- Advertisement -