Firebird SQL连接返回意外值

时间:2015-04-06 15:24:20

标签: sql firebird

我试图加入两张牌桌,但它不会返回倍数值。

表utenvpro

numop  codref div1  div2 qtdenv dataenv     dataprevret
19997  110053  18    P     100  10/03/2015   16/03/2015  
19997  110053  18    M     98   10/03/2015   16/03/2015
19997  110053  18    G     100  10/03/2015   16/03/2015
19997  110053  149   P     100  10/03/2015   16/03/2015
19997  110053  149   M     99   10/03/2015   16/03/2015
19997  110053  149   G     97   10/03/2015   16/03/2015
19997  110053  1000  99    6    10/03/2015   16/03/2015
19997  110053  149   M     4    20/03/2015   25/03/2015
19997  110053  149   G     1    20/03/2015   25/03/2015

表utretpro

numop codref div1 div2 qtdret dataret
19997  110053  18    P     100  17/03/2015
19997  110053  18    M     100  17/03/2015
19997  110053  18    G     100  17/03/2015
19997  110053  149   P     100  17/03/2015
19997  110053  149   M     100  17/03/2015
19997  110053  149   G     100  17/03/2015

查询:

 select utenvpro.codred as Faccao, utenvpro.numop as OP,
     utenvpro.codref as Referencia, utenvpro.div1 as Cor,
     utenvpro.div2 as Tamanho, sum(utenvpro.qtdenv) as QTD_Enviada,
     utretpro.qtdret as QTD_Retorno, utenvpro.dataenv as Envio,
     utenvpro.dataprevret as Prev_Retorno, utretpro.dataret as Retorno
 from utenvpro 
left outer join utretpro on
         utenvpro.codemp=utretpro.codemp
         and utenvpro.codfor=utretpro.codfor
         and utenvpro.codcli=utretpro.codcli
         and utenvpro.numop=utretpro.numop
         and utenvpro.codref=utretpro.codref
         and utenvpro.div1=utretpro.div1
         and utenvpro.div2=utretpro.div2
where numop=19997
group by utenvpro.div2, utenvpro.codref, utenvpro.div1, utenvpro.codred,
     qtd_retorno, utenvpro.numop, utenvpro.dataenv,
     utenvpro.dataprevret, utretpro.dataret
order by op,referencia,envio,cor,tamanho

结果:

Faccao    OP    Referencia    Cor   Tamanho    Qtd_enviada QTD_retorno  envio    prev_retorno    retorno
3002    19997    110053       18       G          100        100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       18       M          98         100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       18       P          100        100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       149      G          97         100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       149      M          99         100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       149      P          100        100    10/03/2015    16/03/2015    17/03/2015
3002    19997    110053       149      G          1          100    20/03/2015    25/03/2015    17/03/2015
3002    19997    110053       149      M          4          100    20/03/2015    25/03/2015    17/03/2015

我不知道这是否清楚明白,但结果的最后两行不应显示。

1 个答案:

答案 0 :(得分:0)

假设我明白你想要的东西......

执行left outer join时,您将从第一个表中获取所有行,并从第二个表中获取匹配的行。如果您想要交叉路口,请使用inner join