[英]when i use sum and count in single query i get incorrect result
用于以下查询:
select sum(j.credits) as credits, count(id.redo) as redos
from operator o inner join jobdetails j on j.opid=o.id
inner join imagedetails id on id.opid=o.id
and im.redo=y group by o.id
如果在J
有3条记录,在ID
4条记录,则应立即将SUM和COUNT与不同的表分开,结果集中的JOIN后会得到3 * 4 = 12条记录,因此得出的总和不正确。 例如:
select o.id, j.sum_credits as credits, id.count_redo as redos
from operator o
inner join
(select opid, sum(credits) sum_credits from jobdetails group by opid) j
on j.opid=o.id
inner join
(select opid, count(redo) count_redo from imagedetails
where redo=y group by opid) id
on id.opid=o.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.