日期:2014-05-17 浏览次数:20438 次
CREATE TABLE T1 --表1
(
SNO INT ,
SLineNO INT,
Qty INT,
ASNO INT,
ASLineNO INT
);
INSERT INTO T1 VALUES
(1001, 1, 10, 1009, 1),
(1001, 2, 10, 1009, 2),
(1002, 1, 10, 1009, 1),
(1003, 1, 10, 2009, 1),
(1003, 2, 10, 2009, 2),
(1004, 1, 10, 2009, 2),
(1005, 1, 10, 3009, 1),
(1005, 2, 10, 4009, 1);
CREATE TABLE T2--表2
( ASNO INT,
ASLineNO INT,
AQty INT,
);
INSERT INTO T2 VALUES
(1009, 1, 10),
(1009, 2, 10),
(2009, 1, 10000),
(2009, 2, 20),
(3009, 1, 10),
(5009, 1, 30),
(6009, 1, 10);
SNO SLineNO Qty ASNO ASLineNO
1001 1 10 1009 1
1001 2 10 1009 2
1002 1 10 6009 1
SELECT *
FROM
(SELECT SNO,SLINENO,SUM(QTY)QTY,ASNO,ASLINENO FROM t1
GROUP BY SNO,SLINENO,ASNO,ASLINENO)A
INNER JOIN T2 B ON A.ASNO=B.ASNO AND A.SLINENO=B.ASLINENO
WHERE A.QTY=B.AQTY
/*
SNO SLINENO QTY ASNO ASLINENO ASNO ASLineNO AQty
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1001 1 10 1009 1 1009 1 10
1001 2 10 1009 2 1009 2 10
1002 1 10