Fórum sql despresando condição #387158

24/09/2010

0

Possuo o seguinte sql:

SQL = "SELECT DISTINCT A.ID_FAZENDA, A.NOME_FAZENDA, " & _
                          "  (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where B.COD_FAZENDA = A.ID_FAZENDA) as VALOR_TOTAL_MAQUINARIOS, " & _
                          "  (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) From REGISTRO_APLICACOES_INSUMO RAI where B.COD_FAZENDA = A.ID_FAZENDA) as VALOR_TOTAL_INSUMOS, " & _
                          "  (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) From REGISTRO_APLICACOES_FUNC RAF where B.COD_FAZENDA = A.ID_FAZENDA) as VALOR_TOTAL_FUNCIONARIOS, " & _
                          "  (Select Coalesce(Sum(RMC.VALOR_TOTAL),0) From RAST_MOV_COLHEITA RMC where C.COD_FAZENDA = A.ID_FAZENDA) as COLHEITA_FUNCIONARIOS, " & _
                          "  (Select Coalesce(Sum(RMM.VALOR_TOTAL),0) From RAST_MOVIMENTACAO_MAQUINARIO RMM where C.COD_FAZENDA = A.ID_FAZENDA) as COLHEITA_MAQUINARIOS, " & _
                          "  (Select Coalesce(Sum(AB.VALOR_TOTAL),0)  From ABASTECIMENTO AB where C.COD_FAZENDA = A.ID_FAZENDA) as ABASTECIMENTO, " & _
                          "  (Select Coalesce(Sum(ME.VALOR),0) From MANUTENCAO_EQUIPAMENTOS ME where C.COD_FAZENDA = A.ID_FAZENDA) as MANUTENCAO_MAQUINARIOS, " & _
                          "  (Select Coalesce(Sum(CT.AREA2),0) From CAD_TALHOES CT where C.COD_FAZENDA = A.ID_FAZENDA) as AREA, " & _
                          "  (Select Coalesce(sum(FP.VALOR_TOTAL),0) From FIN_PAGAMENTOS FP LEFT JOIN FIN_PLANO_CONTAS FPC ON FP.COD_PLANO_CONTAS=FPC.ID_PLANO_CONTAS WHERE FPC.CUSTO='Sim' and B.COD_FAZENDA = A.ID_FAZENDA) as PAGAMENTOS , " & _
                          "  (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where B.COD_FAZENDA = A.ID_FAZENDA)  + (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) From REGISTRO_APLICACOES_INSUMO RAI where B.COD_FAZENDA = A.ID_FAZENDA)+ (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) From REGISTRO_APLICACOES_FUNC RAF where B.COD_FAZENDA = A.ID_FAZENDA)+ (Select Coalesce(Sum(RMC.VALOR_TOTAL),0) From RAST_MOV_COLHEITA RMC where C.COD_FAZENDA = A.ID_FAZENDA)+(Select Coalesce(Sum(RMM.VALOR_TOTAL),0) From RAST_MOVIMENTACAO_MAQUINARIO RMM where C.COD_FAZENDA = A.ID_FAZENDA)+(Select Coalesce(Sum(AB.VALOR_TOTAL),0)  From ABASTECIMENTO AB where C.COD_FAZENDA = A.ID_FAZENDA) + (Select Coalesce(Sum(ME.VALOR),0) From MANUTENCAO_EQUIPAMENTOS ME where C.COD_FAZENDA = A.ID_FAZENDA)" & _
                          "+ (Select Coalesce(sum(FP.VALOR_TOTAL),0) From FIN_PAGAMENTOS FP LEFT JOIN FIN_PLANO_CONTAS FPC ON FP.COD_PLANO_CONTAS=FPC.ID_PLANO_CONTAS WHERE FPC.CUSTO='Sim' and B.COD_FAZENDA = A.ID_FAZENDA) AS CUSTO_GERAL , " & _
                          "  (Select Coalesce(Sum(PC.PREVISTO),0) From PLANO_COLHEITA PC where C.COD_FAZENDA = A.ID_FAZENDA) AS PREVISTO," & _
                          "  (Select Coalesce(Sum(PC1.REALIZADO),0) From PLANO_COLHEITA PC1 where C.COD_FAZENDA = A.ID_FAZENDA) AS REALIZADO " & _
                          " FROM ((CAD_FAZENDA A LEFT JOIN REGISTRO_APLICACOES B ON B.COD_FAZENDA = A.ID_FAZENDA)" & _
                          " LEFT JOIN RAST_MOVIMENTACAO C ON C.COD_FAZENDA=A.ID_FAZENDA)" & _
                          " WHERE A.ID_FAZENDA=" & Identificador_fazenda & _
                          " AND B.COD_TALHAO>=" & txtTalhaoInicial.Text & "AND B.COD_TALHAO<=" & txtTalhaoFinal.Text

Ao adionar a condição: 

 SQL = SQL & " AND B.DATA_INICIAL>= '" & Format(txtDataInicial.Text, "yyyy-mm-dd") & "'"
 slq = SQL & " AND B.DATA_INICIAL<='" & Format(txtDataFinal.Text, "yyyy-mm-dd") & "' "

Não adianta nada, seja filtrando por uma data ou não o resultado final é sempre o mesmo
Paulo Henrique

Paulo Henrique

Responder

Posts

24/09/2010

Wilson Junior

Em primeiro lugar, porque no seu subselect (abaixo) você utiliza um WHERE com tabelas que nem estão relacionadas com a sua tabela principal (REGISTRO_APLICACOES_MAQUINARIO RAM)?
(Select
 Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From 
REGISTRO_APLICACOES_MAQUINARIO RAM where B.COD_FAZENDA = A.ID_FAZENDA) 
as VALOR_TOTAL_MAQUINARIOS,


Assim, sempre terá os mesmos resultados, tanto para este subselect como para os outros.

Espero ter colaborado.
Responder

Gostei + 0

24/09/2010

Anderson

Possuo o seguinte sql:
Ao adionar a condição: 

 SQL = SQL & " AND B.DATA_INICIAL>= '" & Format(txtDataInicial.Text, "yyyy-mm-dd") & "'"
 slq = SQL & " AND B.DATA_INICIAL<='" & Format(txtDataFinal.Text, "yyyy-mm-dd") & "' "

Não adianta nada, seja filtrando por uma data ou não o resultado final é sempre o mesmo


A formatação da data esta correta ?

Por exemplo em consultas diretas pelo flamerobin, eu utilizo  DATA_INICIAL>='dd.mm.aaaa' ?

Sugiro que você coloque um Memoedit na sua aplicação mostrando o comando completo antes da execução, para que você possa copiar e colar a instrução sql em uma ferramenta de consulta ao banco de dados (Flamerobin, IBExpert, Firebird Maestro, ...). 

Executar a instrução sql por uma destas ferramentas irá facilitar a identificação do problema e, se for o caso, desmontar a instrução e ir testando parte por parte. Eu já tive problemas com campos que retornam Null, principalmente em resultados de funções como sum().

Abraços,

Anderson:.
Responder

Gostei + 0

26/09/2010

Emerson Nascimento

o subselect está erado. você NÃO está relacionandoas tabelas corretamente. o correto é relacionar as tabelas que você está manipulando no subselect.

Select
 A.ID_FAZENDA, A.NOME_FAZENDA,
 (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where RAM.COD_FAZENDA = B.ID_FAZENDA) as VALOR_TOTAL_MAQUINARIOS,
 (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) From REGISTRO_APLICACOES_INSUMO RAI where RAI.COD_FAZENDA = B.ID_FAZENDA) as VALOR_TOTAL_INSUMOS,
 (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) From REGISTRO_APLICACOES_FUNC RAF where RAF.COD_FAZENDA = B.ID_FAZENDA) as VALOR_TOTAL_FUNCIONARIOS,
 (Select Coalesce(Sum(RMC.VALOR_TOTAL),0) From RAST_MOV_COLHEITA RMC where RMC.COD_FAZENDA = C.ID_FAZENDA) as COLHEITA_FUNCIONARIOS,
 (Select Coalesce(Sum(RMM.VALOR_TOTAL),0) From RAST_MOVIMENTACAO_MAQUINARIO RMM where RMM.COD_FAZENDA = C.ID_FAZENDA) as COLHEITA_MAQUINARIOS,
 (Select Coalesce(Sum(AB.VALOR_TOTAL),0)  From ABASTECIMENTO AB where AB.COD_FAZENDA = C.ID_FAZENDA) as ABASTECIMENTO,
 (Select Coalesce(Sum(ME.VALOR),0) From MANUTENCAO_EQUIPAMENTOS ME where ME.COD_FAZENDA = C.ID_FAZENDA) as MANUTENCAO_MAQUINARIOS,
 (Select Coalesce(Sum(CT.AREA2),0) From CAD_TALHOES CT where CT.COD_FAZENDA = C.ID_FAZENDA) as AREA,
 (Select Coalesce(sum(FP.VALOR_TOTAL),0) From FIN_PAGAMENTOS FP
  LEFT JOIN FIN_PLANO_CONTAS FPC ON FPC.ID_PLANO_CONTAS=FP.COD_PLANO_CONTAS
  WHERE FPC.CUSTO='Sim' and FP.COD_FAZENDA = B.ID_FAZENDA) as PAGAMENTOS,
 (Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where RAM.COD_FAZENDA = B.ID_FAZENDA)
 + (Select Coalesce(Sum(RAI.VALOR_TOTAL),0) From REGISTRO_APLICACOES_INSUMO RAI where RAI.COD_FAZENDA = B.ID_FAZENDA)
 + (Select Coalesce(Sum(RAF.VALORT_FUNCIONARIOS),0) From REGISTRO_APLICACOES_FUNC RAF where RAF.COD_FAZENDA = B.ID_FAZENDA)
 + (Select Coalesce(Sum(RMC.VALOR_TOTAL),0) From RAST_MOV_COLHEITA RMC where RMC.COD_FAZENDA = C.ID_FAZENDA)
 + (Select Coalesce(Sum(RMM.VALOR_TOTAL),0) From RAST_MOVIMENTACAO_MAQUINARIO RMM where RMM.COD_FAZENDA = C.ID_FAZENDA)
 + (Select Coalesce(Sum(AB.VALOR_TOTAL),0)  From ABASTECIMENTO AB where AB.COD_FAZENDA = C.ID_FAZENDA)
 + (Select Coalesce(Sum(ME.VALOR),0) From MANUTENCAO_EQUIPAMENTOS ME where ME.COD_FAZENDA = C.ID_FAZENDA)
 + (Select Coalesce(sum(FP.VALOR_TOTAL),0) From FIN_PAGAMENTOS FP
    LEFT JOIN FIN_PLANO_CONTAS FPC ON FPC.ID_PLANO_CONTAS=FP.COD_PLANO_CONTAS
    WHERE FPC.CUSTO='Sim' and FP.COD_FAZENDA = A.ID_FAZENDA) AS CUSTO_GERAL,
 (Select Coalesce(Sum(PC.PREVISTO),0) From PLANO_COLHEITA PC where PC.COD_FAZENDA = C.ID_FAZENDA) AS PREVISTO,
 (Select Coalesce(Sum(PC1.REALIZADO),0) From PLANO_COLHEITA PC1 where PC1.COD_FAZENDA = A.ID_FAZENDA) AS REALIZADO
FROM
 CAD_FAZENDA A
LEFT JOIN
 REGISTRO_APLICACOES B ON B.COD_FAZENDA = A.ID_FAZENDA)
LEFT JOIN
 RAST_MOVIMENTACAO C ON C.COD_FAZENDA=A.ID_FAZENDA)
WHERE
 A.ID_FAZENDA=" & Identificador_fazenda & _
 AND B.COD_TALHAO>=" & txtTalhaoInicial.Text & " AND B.COD_TALHAO<=" & txtTalhaoFinal.Text

você precisa avaliar também se essas tabelas serão relacionadas à tabela A, B ou C.

dependendo da versão do FB que você estiver usando essa instrução poderá ser melhorada.

Responder

Gostei + 0

27/09/2010

Paulo Henrique

Emerson, mas ai que ta o problema por exemplo na subselect:

 "(Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where RAM.COD_FAZENDA = B.ID_FAZENDA) as VALOR_TOTAL_MAQUINARIOS," & _


A tabela REGISTRO_APLICACOES_MAQUINARIO não possui o campo codigo da fazenda pois ela é vinculada  a tabela REGISTRO_APLICACOES e o codigo da fazenda só o gravado na tabela principal no caso REGISTRO_APLICACOES
Responder

Gostei + 0

27/09/2010

Emerson Nascimento

isso foi apenas um exemplo. eu não tenho a estrutura das suas tabelas, então não tenho como te responder com 100% de eficácia.

você precisa vincular as tabelas corretamente e seu problema será resolvido.

por exemplo:

"(Select Coalesce(Sum(RAM.VALORT_MAQUINARIOS),0) From REGISTRO_APLICACOES_MAQUINARIO RAM where RAM.ID_REGISTRO_APLICACOES = B.ID_REGISTRO_APLICACOES) as VALOR_TOTAL_MAQUINARIOS," & _

suponho que a tabela REGISTRO_APLICACOES_MAQUINARIO (RAM) seja vinculada à tabela REGISTRO_APLICACOES (B)
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar