我有这样的代码:
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 need ever B.plc_nomeConta, B.plc_classificador, B.plc_id出现的行。
发布于 2012-11-14 03:31:50
您的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)
或者改进你的需求。
发布于 2012-11-15 01:24:50
我已经解决了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 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 感谢每一个人的帮助
https://stackoverflow.com/questions/13367385
复制相似问题