1

我有这个代码:

select
  B.plc_nomeConta, B.plc_classificador, B.plc_id,
  A.cap_idPlanoContasFin, SUM(A.cap_valorfatura) as Total     
from
  tbl_PlanoFinanceiro B
  left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin
   /*  where A.cap_idEmpresa like 2*/
group by
  B.plc_nomeConta,
  B.plc_classificador,
  B.plc_id,
  A.cap_idPlanoContasFin 

此代码返回 185 行,

(-) COFINS     10.01.01.01.004  330  330   971090,97
(-) ICMS       10.01.01.01.002  328  328   1378407,11
(-) IMPOSTOS   10.01.00.00.000  324  NULL  NULL
(-) IMPOSTOS   10.01.01.00.000  325  NULL  NULL
(-) IMPOSTOS   10.01.01.01.000  326  NULL  NULL
(-) ISS        10.01.01.01.001  327  327   1000960,59
(-) PIS        10.01.01.01.003  329  329   240600,27

但是当我取消注释时where /* where A.cap_idEmpresa like 2*/,只返回 A.cap_idPlanoContasFin is not null, In needB.plc_nomeConta, B.plc_classificador, B.plc_id出现的行。

4

2 回答 2

4

您的WHERE过滤器正在将 转换LEFT OUTER JOININNER JOIN.

本质上,您是在说“向我显示左侧的所有记录,并且仅显示右侧匹配cap_idEmpresa且值为2”的记录。

这意味着您只显示匹配的记录,即任何不匹配的记录在该字段中INNER JOIN都不能有值。2

要更正,您需要考虑 null:

WHERE (A.cap_idEmpresa like 2 OR A.cap_idEmpresa IS NULL)

或细化您的要求。

于 2012-11-13T19:31:50.437 回答
0

我已经用 JNK 和 cadrell0 解决了,使 A.Cap_idempresa like 2 成为 JOin 的一部分,感谢这里的代码

        alter proc Ntrimestre (@emp integer,@inicio datetime,@fim datetime) as
        select  B.plc_nomeConta, B.plc_classificador ,B.plc_id ,
        A.cap_idPlanoContasFin, SUM (A.cap_valorfatura) as Total
        from tbl_PlanoFinanceiro B
        left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin 
        and   A.cap_idEmpresa like @emp
        and A.cap_vencfatura <= convert(datetime,@fim,1) 
        and cap_vencfatura >= convert(datetime,@inicio,1) 
        group by B.plc_nomeConta, B.plc_classificador ,B.plc_id, A.cap_idPlanoContasFin
       order by B.plc_classificador 

与 where 的另一种方式:

       alter proc NtrimestreMzFSant (@inicio datetime,@fim datetime) as
       select  B.plc_nomeConta, B.plc_classificador ,B.plc_id ,
        A.cap_idPlanoContasFin, SUM (A.cap_valorfatura) as Total
       from tbl_PlanoFinanceiro B
       left outer join erp_contaspagar A on B.plc_id = A.cap_idPlanoContasFin  
        and A.cap_vencfatura <= convert(datetime,@fim,1) 
         and A.cap_vencfatura >= convert(datetime,@inicio,1) 
        where B.plc_tipo <> 'Sintética' and A.cap_idEmpresa =2 or A.cap_idEmpresa=2234 
        group by B.plc_nomeConta, B.plc_classificador ,B.plc_id, A.cap_idPlanoContasFin
        order by B.plc_classificador 

感谢每个人的帮助

于 2012-11-14T17:24:50.180 回答