March 10, 2009 at 11:30 am
I am trying to join two tables. The columns have to do with access codes. Table 1 defines the access code to a particular screen. Table 2 defines the users with the different access codes.
Table 1
Screen 1234-A
Screen 2345-B
Screen 3456-C
Table 2
User1-AB
User2-A
User3-C
User4-BAC
User5-CB
I want to create a join to show all the users who have access to Screen 1234. As I am trying to show, User1 and User 4 have multiple access rights and User 2 has access to only Screen 1234. How do I join the two tables on the access codes if the access codes in table two can have multiple codes.
Here is my actual script. Function code being the screen code.
select a.FunctionCode, a.FunctionDescription, a.MenuCode, a.AccessCodes, b.AccessCodes, b.UserName
from FS_FunctionCode a left join FS_UserAccess b on b.AccessCodes like ('%'+a.AccessCodes+'%')
order by a.MenuCode, a.FunctionCode
I was thinking of some kind of LIKE join, or maybe a variable, but I can't get my head around it. The result I get is a match up of all the users with one access code, but none with the multiple codes. I had one of those late evenings and early mornings with a new application last night.
Thanks
March 10, 2009 at 11:38 am
Instead of doing the AB, ABC thing, I'd normalize it and have one per row. That'll make the whole thing much easier to work with.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 10, 2009 at 12:04 pm
I have to agree with the comment above about normalistation, you will be in for a lot of issues down the track with this sort of design
March 10, 2009 at 12:07 pm
correct me if I'm wrong, but the character after the '-'(A,B etc) is the screen...so if you get that substring, you could check for the charindex on each user:
try joining on this instead and tell me if that works:
left join FS_UserAccess b on CHARINDEX(SUBSTRING(b.AccessCodes,CHARINDEX('-',b.AccessCodes),a.AccessCodes) > 0
Lowell
March 10, 2009 at 12:39 pm
I'll mess with the CHARINDEX. I think that is my best option. There isn't a - in the field, just the codes. As far as normalizing, that's not possible as this is coming from a control table created by the application, unless someone is willing to explain to me how to unconcanate 1-30 characters and I can dump it into a temp table.
Thanks
March 10, 2009 at 12:42 pm
Do you have a Numbers/Tally table? If so, then it can be normalized pretty easily.
select substring(MyCol, Number, 1)
from dbo.MyTable
inner join dbo.Numbers
on Number <= len(MyCol);
Something like that will split up a string pretty darn fast.
If you don't have a Numbers table, can still be done by building one on the fly in a CTE. Need to see how to do that?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
March 10, 2009 at 1:08 pm
I got it with the CHARINDEX. As soon as I saw your post I knew that was the key, I just couldn't get it to work. Turned out the problem was that the table with the access code assigned to the page had 2 dozen or so spaces after the code. So except for the A to A matchups, nothing else was showing up until I did a rtrim on a.AccessCodes. So here is what the final was.
select a.FunctionCode, a.FunctionDescription, a.MenuCode, b.UserName
from FS_FunctionCode a left join FS_UserAccess b on (CHARINDEX(rtrim(a.AccessCodes),b.AccessCodes)) > 0
order by a.MenuCode, a.FunctionCode
Thanks for your help, Lowell.
March 10, 2009 at 3:25 pm
Hi
Sure the code Lowell posted works fine for your specified problem. But the other guys will inform you that this kind of database-/table-desing may bring you into much more problems while development.
Maybe have a look to this link http://en.wikipedia.org/wiki/Database_normalization . I'm quiet sure there are also many articles on this page about "database normalization".
Greets
Flo
March 10, 2009 at 5:16 pm
i am not designing a table or a database. the problem was not how the table was designed. the problem was how to use the tables that i have been given.
March 10, 2009 at 5:26 pm
[font="Verdana"]You could use substring and UNPIVOT to take the data from the form
User1-AB
to:
User1 A
User1 B
That would then simplify the join. I'd do the UNPIVOT as a CTE.
[/font]
March 10, 2009 at 8:40 pm
adams.squared (3/10/2009)
i am not designing a table or a database. the problem was not how the table was designed. the problem was how to use the tables that i have been given.
True enough and you said so. Folks are just concerned enough to tell you anyway... you might even tell the dummies that designed the app about it... or, not. 😉
Shifting gears, even though I'm a Tally table lover, I believe that Lowell's good CHARINDEX code will probably beat it.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply