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 |
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-09-09 : 17:43:24
|
| I have three tables as follows :TableA ID MatchID1 status 1 0001 NULL 2 0002 NULL 3 0003 NULLTableB ID MatchID1 MatchID2 1 002 120 2 002 120 3 002 141 4 003 150 5 003 150 6 003 140TableC ID MatchID1 MatchID2 1 002 120 2 002 120 3 002 120 4 003 150 5 003 150 6 003 150Things to consider: populate tableA status with 'FAIL' when following logic is not met:>1. All MatchID2 for same MatchID1 in table C should be equal to the MINIMUM maatchID2 for same MatchID1 for tableB, if not populate 'FAIL' in the corresponding MatchID1 in tableAtableC : for 002, ID2 = 120 (this should match to minimum ID2 of TableB which is 120, so its not a fail.)tableC: for 003, all matchID2 must equal to min ID2 of tableB which is 140, but its not , so its a fail in tableA for corresponding matchID1 in tableA (0003)so expected table A must be : ID MatchID1 status 1 0001 NULL 2 0002 NULL 3 0003 FAILplease note that data might be different but it has to obey the logic.Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-10 : 00:50:46
|
| update aset a.status = 'FAIL'FROM TableA a INNER JOIN (SELECT matchID1,MIN(matchID2) AS MinID2 FROM TableB GROUP BY matchID1) bON '0'+ b.matchID1 = a.matchID1INNER JOIN (SELECT MatchID1,MAX(MatchID2) AS MaxID2 FROM TableC GROUP BY MatchID1 HAVING MAX(MatchID2) = MIN(MatchID2))cON c.MatchID1 = b.MatchID1AND c.MaxID2 = b.MinID2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ben_53
Yak Posting Veteran
67 Posts |
Posted - 2011-09-12 : 14:35:56
|
| @visakh16: thanks for reply. However the query is not according to the logic.for the given sample data, it should fail ID3 in tableA but your query is failing ID2. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-13 : 00:30:42
|
sorry its should have beenupdate aset a.status = 'FAIL'FROM TableA a INNER JOIN (SELECT matchID1,MIN(matchID2) AS MinID2FROM TableBGROUP BY matchID1) bON '0'+ b.matchID1 = a.matchID1INNER JOIN (SELECT MatchID1,MAX(MatchID2) AS MaxID2FROM TableCGROUP BY MatchID1HAVING MAX(MatchID2) = MIN(MatchID2))cON c.MatchID1 = b.MatchID1AND c.MaxID2 <> b.MinID2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|