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 |
KabirPatel
Yak Posting Veteran
54 Posts |
Posted - 2008-01-11 : 06:53:05
|
Hi,I have some tables as follows:TableATableAID Title--------------------------------T1 Test 1T2 Test 2T3 Test 3TableBTableBID Title--------------------------------G1 Bad1G2 Bad2G3 Bad3TableATableB (this is a one to one relationship between TableA and TableB)TableAID TableBID--------------------------------T1 G2T2 G1T3 G3TableARoles (this is a one to many relationship between TableA records and "Roles")TableAID Role--------------------------------T1 Role1T1 Role2T2 Role1T2 Role3TableBRoles (this is a one to many relationship between TableB records and "Roles")TableBID Role--------------------------------G2 Role1G2 Role2G1 Role1G1 Role4I am having trouble writing a query to produce the following output:TableBID TableAID Desc---------------------------------------------G2 T1 Full Role MatchG1 T2 Partial Role Matchin other words the query should list the matches between TableA and TableB and should also specify whether the Roles mapped to the TableA record match the Roles mapped to the TableB record.e.g. T1 above has the Roles "Role1" and "Role2" and so does G2. As such they have a "full match".Thanks in advanceKabir |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-01-11 : 07:35:28
|
select t.TableAID, t.TableBID,case when max(case when arole+brole is null then 1 else 0 end) = 0 then 'full' else 'partial' endfromTableATableB tleft join(select TableATableB.TableAID, TableATableB.TableBID, arole = TableARoles.Rolefrom TableATableB join TableARoles on TableARoles.TableAID = TableATableB.TableAID) taon t.TableAID = ta.TableAIDand t.TableBID = ta.TableBIDleft join (select TableATableB.TableAID, TableATableB.TableBID, brole = TableBRoles.Rolefrom TableATableB join TableBRoles on TableBRoles.TableBID = TableATableB.TableBID) tbon t.TableAID = tb.TableAIDand t.TableBID = tb.TableBIDand ta.arole = tb.brolegroup by t.TableAID, t.TableBIDhaving max(coalesce(arole, brole)) = max(coalesce(arole, brole))==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|