我有以下SQL语句:
select
a.desc
,sum(bdd.amount)
from t_main c
left outer join t_direct bds on (bds.repid=c.id)
left outer join tm_defination def a on (a.id =bds.sId)
where c.repId=1000000134
group by a.desc;
当我运行它时,我得到了以下结果:
desc amount
NW 12.00
SW 10
当我尝试添加另一个左外连接来获取另一组值时:
select
a.desc
,sum(bdd.amount)
,sum(i.amt)
from t_main c
left outer join t_direct bds on (bds.repid=c.id)
left outer join tm_defination def a on (a.id =bdd.sId)
left outer join t_ind i on (i.id=c.id)
where c.repId=1000000134
group by a.desc;
它基本上将数量字段加倍,如下所示:
desc amount amt
NW 24.00 234.00
SE 20.00 234.00
虽然结果应该是:
desc amount amt
NW 12.00 234.00
SE 10.00 NULL
我该怎么解决这个问题?
a
字段进行分组。尝试在SELECT
部分和GROUP BY
部分都添加c.id
。 - GregHNZ