left outer join sql server sum duplicate

时间:2013-11-28 12:52:40

标签: sql-server

我在链接2个表时遇到问题,以获得每个表的总和结果:

select cli.codigo, cli.nome, sum(isnull(det1.val_venda,0)) as Vendas_Mes1,
   sum(isnull(det2.val_venda,0)) as Vendas_Mes2
   from clientes cli
   right outer join facmovim fac1 on fac1.clinro=cli.codigo  and
                             fac1.ano='2012' and month(fac1.datemi) =12
   right outer join facmovdet det1 on det1.tipdoc=fac1.tipdoc and
                              det1.serie=fac1.serie and
                              det1.ano=fac1.ano and
                              det1.docnro=fac1.docnro and det1.tiporeg = 'A'
   right outer join facmovim fac2 on fac2.clinro=cli.codigo and
                             fac2.ano='2013' and month(fac2.datemi) =3
   right outer join facmovdet det2 on det2.tipdoc=fac2.tipdoc and
                              det2.serie=fac2.serie and
                              det2.ano=fac2.ano and
                              det2.docnro=fac2.docnro and det2.TipoReg = 'A'

    where cli.codigo = 1 
    group by cli.codigo, cli.nome

表det1有2条记录,表det2有3条记录。每个表的总和结果重复5次。 你能帮我解决这个问题吗? 非常感谢

2 个答案:

答案 0 :(得分:0)

您可以通过此代码从单个表中获取Duplicates行,请尝试

  with cte as
  (
  select *,rn=ROW_NUMBER() over (PARTITION by autoid order by id ) from tablename
  )
  select * from cte where rn>1

答案 1 :(得分:0)

我不喜欢在同一个查询中没有over子句的重复和。 我总是有奇怪的结果。

你可以尝试这样的事情:

select cli.codigo, cli.nome, sum(isnull(det1.val_venda,0)) as Vendas_Mes1,
sum(isnull(det2.val_venda,0)) as Vendas_Mes2
from (
select cli.codigo, cli.nome, sum(isnull(det1.val_venda,0)) as Vendas_Mes1,
0 as Vendas_Mes2
from clientes cli
right outer join facmovim fac1 on fac1.clinro=cli.codigo  and
                          fac1.ano='2012' and month(fac1.datemi) =12
right outer join facmovdet det1 on det1.tipdoc=fac1.tipdoc and
                           det1.serie=fac1.serie and
                           det1.ano=fac1.ano and
                           det1.docnro=fac1.docnro and det1.tiporeg = 'A'
where cli.codigo = 1 
union all
select cli.codigo, cli.nome, 0 as Vendas_Mes1,
sum(isnull(det1.val_venda,0)) as Vendas_Mes2
from clientes cli
right outer join facmovim fac1 on fac1.clinro=cli.codigo  and
                          fac1.ano='2013' and month(fac1.datemi) =12
right outer join facmovdet det1 on det1.tipdoc=fac1.tipdoc and
                           det1.serie=fac1.serie and
                           det1.ano=fac1.ano and
                           det1.docnro=fac1.docnro and det1.tiporeg = 'A'
where cli.codigo = 1 
) p
group by cli.codigo, cli.nome

联盟的表现一点也不差,而且更加确定,特别是如果你没有加入同一张桌子但是不同以产生不同的金额。

在你的情况下你也许也可以这样做

select cli.codigo, cli.nome, sum(case when fac1.ano = '2012' then isnull(det1.val_venda,0), else 0 end) as Vendas_Mes1,
sum(case when fac1.ano = '2013' then isnull(det2.val_venda,0) else 0 end) as Vendas_Mes2
from clientes cli
right outer join facmovim fac1 on fac1.clinro=cli.codigo  and
                          month(fac1.datemi) =12
right outer join facmovdet det1 on det1.tipdoc=fac1.tipdoc and
                           det1.serie=fac1.serie and
                           det1.ano=fac1.ano and
                           det1.docnro=fac1.docnro and det1.tiporeg = 'A'
where cli.codigo = 1 
group by cli.codigo, cli.nome

我的代码不完整,但逻辑在里面:)