日期:2014-05-17 浏览次数:20754 次
LotCode WipID 792 28 793 30 796 33 ... ...
LotCode WipID 85 28 793 28 88 28 89 28 796 30 85 30 26 33 ... ...
CREATE TABLE #T1 ( LotCode INT, WipID INT ) CREATE TABLE #T2( LotCode INT, WipID INT ) INSERT INTO #T1 ( LotCode, WipID ) VALUES ( 792,28),(793,30),(796,33) INSERT INTO #T2 ( LotCode, WipID ) VALUES (85,28),(793,28),(88,28),(89,28),(796,30),(85,30),(26,33) ;WITH TEMP AS( SELECT A.LotCode,B.LotCode l2,A.WipID FROM #T1 A JOIN #T2 B ON A.WipID = B.WipID WHERE A.LotCode = 792 UNION ALL SELECT C.l2 AS LotCode,E.LotCode l2,E.WipID FROM TEMP C JOIN #T1 B ON C.l2 = B.LotCode JOIN #T2 E ON B.WipID = E.WipID ) SELECT LotCode FROM temp WHERE LotCode <> 792 GROUP BY LotCode DROP TABLE #T1,#T2