日期:2014-05-17 浏览次数:20882 次
--Create CREATE TABLE tests(MC VARCHAR(20),A VARCHAR(20),B VARCHAR(20),LHBH VARCHAR(20),LX INT ) --insert INSERT INTO tests SELECT '仓库A<->仓库C','003','004','003004','1' UNION ALL SELECT '仓库B<->仓库C','001','004','001004','1' UNION ALL SELECT '仓库C<->仓库A','004','003','004003','2' UNION ALL SELECT '仓库C<->仓库B','004','001','004001','2' UNION ALL SELECT '仓库A<->仓库B','003','001','003001','2' --select SELECT * FROM tests --Query WITH t AS (SELECT t1.*,ROW_NUMBER() OVER (PARTITION BY t1.LX ORDER BY t1.LX) AS BH FROM tests t1 LEFT JOIN tests t2 ON t1.a=t2.b AND t1.b=t2.a WHERE t2.a IS NOT NULL) SELECT * FROM t WHERE bh =1 --drop DROP TABLE tests MC A B LHBH LX BH -------------------- -------------------- -------------------- -------------------- ----------- -------------------- 仓库A<->仓库C 003 004 003004 1 1 仓库C<->仓库A 004 003 004003 2 1