I have a table containing user-account permissions and I’m trying to write a query to return one row for each user-account combination.
Here is what I have.
CltKey AcctKey TranTypeID Access
10 2499 10 0
10 2499 11 1
10 2499 12 1
10 2764 10 1
10 2764 11 1
10 2764 12 0
Here is what I’d like to have.
CltKey AcctKey TranTypeID1 Access1 TranTypeID2 Access2 TranTypeID3 Access3
10 2499 10 0 11 1 12 1
10 2764 10 1 11 1 12 0
Or even better something like this.
CltKey AcctKey HasTranTypeID1 HasTranTypeID2 HasTranTypeID3
10 2499 0 1 1
10 2764 1 1 0
I have tried doing a self join, but I keep getting multiple rows for each TranTypeID. One with it equal to 0 and another with it equal to 1. I have also tried using nested “Select” statements, but the performance is horrible. Does anyone have an idea on how to do this?
Thanks.
Edit: Unfortunately, this has to work in SQL 2000.
It’s been a while since I used SQLServer 2000, but this will probably work.
If not, try this:
and then get your results from this
Note that this will tell you a (cltkey, acctkey) has access (of a particular type) if any row for that tuple of (cltkey, acctkey) has access for that particular type. That is, it’s essentially a row-wise
OR.If all rows for that tuple must have access for that tuple to have access, that is, if you want a row-wise
AND, you’ll need to do this: