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.
Author |
Topic |
singularity
Posting Yak Master
153 Posts |
Posted - 2008-07-17 : 19:50:48
|
I've got two tables that looks like this:Role_Cd Role-------------1 A2 B4 C8 D16 E32 F64 Getc.User_ID Role_Cd-----------------1 42 123 49The rule is that in the second table the Role_Cd will be a sum of the user's roles from the first table. A user could have an unlimited number of roles. My goal is to break down that number and show a row for each role, like so:User_ID Role------------1 C2 C2 D3 A3 E3 FIs this possible? |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2008-07-17 : 23:32:26
|
Assuming that a user can only have a role one time then yes it is possible.Look at case"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-18 : 02:59:55
|
[code]DECLARE @role TABLE( role_cd int, role CHAR(1))INSERT INTO @roleSELECT 1, 'A' UNION ALLSELECT 2, 'B' UNION ALLSELECT 4, 'C' UNION ALLSELECT 8, 'D' UNION ALLSELECT 16, 'E' UNION ALLSELECT 32, 'F' UNION ALLSELECT 64, 'G'DECLARE @user TABLE( user_id int, role_cd int)INSERT INTO @userSELECT 1, 4 UNION ALLSELECT 2, 12 UNION ALLSELECT 3, 49SELECT u.user_id, r.roleFROM @user u INNER JOIN @role r ON r.role_cd & u.role_cd <> 0ORDER BY u.user_id, r.role/*user_id role ----------- ---- 1 C 2 C 2 D 3 A 3 E 3 F(6 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
singularity
Posting Yak Master
153 Posts |
Posted - 2008-07-18 : 09:02:48
|
KH, that was awesome! Thanks a lot. |
 |
|
|
|
|
|
|