我有这段代码:
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
所显示的行,{I}需要B.plc_nomeConta, B.plc_classificador, B.plc_id
。
答案 0 :(得分:4)
您的WHERE
过滤器正在将LEFT OUTER JOIN
转换为INNER JOIN
。
基本上你是说“在左边显示所有记录,只在右边的记录中匹配并且cap_idEmpresa
值为2
”。
这意味着您只显示匹配的记录,即INNER JOIN
- 任何不匹配的记录在该字段中的值不能为2
。
要纠正您需要考虑null:
WHERE (A.cap_idEmpresa like 2 OR A.cap_idEmpresa IS NULL)
或改进您的要求。
答案 1 :(得分:0)
我已经用JNK和cadrell0解决了,让A.Cap_idempresa像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
另一种方式:
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
感谢每个人的帮助