日期:2014-05-18  浏览次数:20673 次

求一出库总数和出库明细表数量合计对不平的SQL--在线等!
aa表(出库物料)
number(id) name(物料名称) ckzs(出库总数)
1 A 5  
2 B 0
3 C 11
4 D 0
bb表(出库明细)
recnumber(出库物料ID) cksl(出库数量)
1 1
1 3
3 2
1 2 
3 5
4 6

列出aa表中哪些物料出库总数和bb表出库明细数量合计对不起来的SQL语句

------解决方案--------------------
探讨
引用:
SQL code--try
SELECT * FROM AA A WHERE NOT EXISTS(SELECT 1 FROM (SELECT recnumber,SUM(CKSL)CKSL FROM AA GROUP BY recnumber)T WHERE T.recnumber=A.number
AND T.CKSL!=A.CKZS )


SQL code--表名寫錯
SELECT * FROM AA A WHERE NOT EXISTS(SELECT 1 FROM
(SELECT recnumber,SUM(CKSL)CKSL FROM BB GROUP BY recnumber)T WHERE T.recnumber=A.number
AND T.CKSL!=A.CKZS )

------解决方案--------------------
SQL code

CREATE TABLE TESTTB1(numberid INT ,name CHAR(5),  ckzs INT)
INSERT TESTTB1
SELECT 1,          'A',              5    UNION
SELECT 2,          'B',              0 UNION
SELECT 3,          'C',              11 UNION
SELECT 4,          'D',             0 
CREATE TABLE TESTTB2(recnumber INT,cksl INT)
INSERT TESTTB2
SELECT 1 ,                       1 UNION
SELECT 1  ,                      3 UNION
SELECT 3   ,                     2 UNION
SELECT 1  ,                      2 UNION
SELECT 3  ,                      5 UNION
SELECT 4 ,                       6 


--SELECT * FROM TESTTB1
--SELECT * FROM TESTTB2

SELECT TESTTB1.* FROM TESTTB1,(SELECT RECNUMBER,SUM(CKSL)CKSL FROM TESTTB2 GROUP BY RECNUMBER)AS T
 WHERE TESTTB1.numberid=T.RECNUMBER AND CKZS<>CKSL