2

这是两个查询,返回多条记录,那么有什么解决方案可以从另一个结果中减去一个结果的每一行数据?

查询1:

SELECT COUNT(*) As AbhidayNotPaidSansthan
 FROM PanjikaranMaster GROUP BY Zila

查询2:

SELECT COUNT(*) as anps FROM AbhidayMaster,PanjikaranMaster
    WHERE PanjikaranMaster.OfficeRegId=AbhidayMaster.OfficeRegId AND
    AbhidayMaster.DipositDate Between ('2012-09-19') AND ('2012-09-24')
GROUP BY PanjikaranMaster.Zila
4

1 回答 1

2

试试这个查询,它会解决你的问题:

SELECT ISNULL(a.cnt,0) -  ISNULL(b.cnt,0) AS CNT,a.Zila
FROM
    (SELECT Zila,COUNT(*) as cnt As AbhidayNotPaidSansthan
    FROM PanjikaranMaster GROUP BY Zila) a
    LEFT JOIN 
    (SELECT Zila,COUNT(*) as cnt FROM AbhidayMaster,PanjikaranMaster
    WHERE PanjikaranMaster.OfficeRegId=AbhidayMaster.OfficeRegId AND
    AbhidayMaster.DipositDate Between ('2012-09-19') AND ('2012-09-24')
    GROUP BY PanjikaranMaster.Zila) b 
    ON a.Zila = b.Zila

我在SQLFIDDLE中做过类似的测试

于 2012-09-25T05:52:50.297 回答