我想要不同ID的金额总和
我的查询就像那样
somecondition
使用DISTINCT标准只有3个结果: 42978 8606 194.00 2016-11-29 44138 8606 194.00 2016-12-21 44139 8606 6.00 2016-12-21
如果我在没有DISTINCT关键字的情况下午餐相同的查询,则为6行 42978 8606 194.00 2016-11-29 42978 8606 194.00 2016-11-29 44138 8606 194.00 2016-12-21 44138 8606 194.00 2016-12-21 44139 8606 6.00 2016-12-21 44139 8606 6.00 2016-12-21
我想创建一个SQL查询生成器,因此我找到了一个生成非常简单的查询代码的解决方案。 我的第一个想法是:
SELECT DISTINCT pyId, paId, (pyCredit) As Credit, pyDate
FROM T20
INNER JOIN T30 ON T20.paId = T30.ttLien20
INNER JOIN T49 ON T30.id30 = T49.tEchLien30
INNER JOIN T50 ON T30.id30 = T50.acLien30
INNER JOIN T60 ON T49.techId = T60.ecId49 OR T50.acId = T60.ecLien50
INNER JOIN T70 ON T60.ecId = T70.pyLien60
WHERE pyId IN (42978,44138,44139)
但是这种情况是每行加两次!所以全球总和更糟糕。
即使我做了类似的事情,结果仍然很好
SELECT DISTINCT pyId, paId, SUM(pyCredit) As Credit, pyDate
FROM T20
INNER JOIN T30 ON T20.paId = T30.ttLien20
INNER JOIN T49 ON T30.id30 = T49.tEchLien30
INNER JOIN T50 ON T30.id30 = T50.acLien30
INNER JOIN T60 ON T49.techId = T60.ecId49 OR T50.acId = T60.ecLien50
INNER JOIN T70 ON T60.ecId = T70.pyLien60
WHERE pyId IN (42978,44138,44139)
但更难以概括。那么有更好的方法来实现我的目标吗?
祝你好运