Comparando colunas

Firebird

02/01/2014

Bom Dia! Feliz Ano Novo!

Estou tendo dificuldade para montar me select. Ele já esta assim
SELECT
    T1.CODIGO_MOVIMENTACAO,
    T1.VALOR_NOTA, 
    T1.DATA_EMISSAO,
    T1.VALOR_VENCIMENTO1,
    T1.DATA_VENCIMENTO1,
    T1.VALOR_PAGAMENTO1,
    T1.DATA_PAGAMENTO1,
    T1.VALOR_JUROS1,
    T1.VALOR_DESCONTO1,
    T2.NOME_FORNECEDOR,
    ((coalesce(T1.VALOR_VENCIMENTO1,'0') + coalesce(T1.VALOR_VENCIMENTO2,'0') +
      coalesce(T1.VALOR_VENCIMENTO3,'0') + coalesce(T1.VALOR_VENCIMENTO4,'0') + 
      coalesce(T1.VALOR_VENCIMENTO5,'0') + coalesce(T1.VALOR_VENCIMENTO6,'0') +
      coalesce(T1.VALOR_VENCIMENTO7,'0') + coalesce(T1.VALOR_VENCIMENTO8,'0') +
      coalesce(T1.VALOR_VENCIMENTO9,'0') + coalesce(T1.VALOR_VENCIMENTO10,'0') +
      coalesce(T1.VALOR_VENCIMENTO11,'0')+ coalesce(T1.VALOR_VENCIMENTO12,'0')) +
     (coalesce(T1.VALOR_JUROS1,'0') + coalesce(T1.VALOR_JUROS2,'0') +
      coalesce(T1.VALOR_JUROS3,'0') + coalesce(T1.VALOR_JUROS4,'0') +
      coalesce(T1.VALOR_JUROS5,'0') + coalesce(T1.VALOR_JUROS6,'0') +
      coalesce(T1.VALOR_JUROS7,'0') + coalesce(T1.VALOR_JUROS8,'0') +
      coalesce(T1.VALOR_JUROS9,'0') + coalesce(T1.VALOR_JUROS10,'0') +
      coalesce(T1.VALOR_JUROS11,'0')+ coalesce(T1.VALOR_JUROS12,'0'))-
     (coalesce(T1.VALOR_PAGAMENTO1,'0') + coalesce(T1.VALOR_PAGAMENTO2,'0') +
      coalesce(T1.VALOR_PAGAMENTO3,'0') + coalesce(T1.VALOR_PAGAMENTO4,'0') +
      coalesce(T1.VALOR_PAGAMENTO5,'0') + coalesce(T1.VALOR_PAGAMENTO6,'0') +
      coalesce(T1.VALOR_PAGAMENTO7,'0') + coalesce(T1.VALOR_PAGAMENTO8,'0') +
      coalesce(T1.VALOR_PAGAMENTO9,'0') + coalesce(T1.VALOR_PAGAMENTO10,'0') +
      coalesce(T1.VALOR_PAGAMENTO11,'0')+ coalesce(T1.VALOR_PAGAMENTO12,'0'))-
     (coalesce(T1.VALOR_DESCONTO1,'0') + coalesce(T1.VALOR_DESCONTO2,'0') +
      coalesce(T1.VALOR_DESCONTO2,'0') + coalesce(T1.VALOR_DESCONTO4,'0') +
      coalesce(T1.VALOR_DESCONTO5,'0') + coalesce(T1.VALOR_DESCONTO6,'0') +
      coalesce(T1.VALOR_DESCONTO7,'0') + coalesce(T1.VALOR_DESCONTO8,'0') +
      coalesce(T1.VALOR_DESCONTO9,'0') + coalesce(T1.VALOR_DESCONTO10,'0') +
      coalesce(T1.VALOR_DESCONTO11,'0')+ coalesce(T1.VALOR_DESCONTO12,'0'))) AS SALDO
FROM
    CONTAS_PAGAR T1,
    FORNECEDORES T2
WHERE 
    T1.FORNECEDOR = T2.NOME_FORNECEDOR

só que nesse trecho
AND SALDO > 0  
AND SALDO < T1.VALOR_NOTA 

da o erro
Dynamic SQL Error.
SQL error code = -206.
Column unknown.
SALDO.
At line 42, column 5.
que é referente a Coluna SALDO. Como faço para compara essa coluna com as outras
Eduardo Mendonça

Eduardo Mendonça

Curtidas 0

Respostas

Leandro Chiodini

Leandro Chiodini

02/01/2014

Boa Tarde.

Aonde que esta linha que esta dando erro esta sendo chamada?
Pois no select principal não localizei.

Outra coisa,
Como voce usa mts campos pra guardar a mesma informação,
poderia sofrer uma alteração na modelagem para evitar a utilização de tantos campos iguais,

att,
Leandro
GOSTEI 0
Eduardo Mendonça

Eduardo Mendonça

02/01/2014

esse Selecte é assim
SELECT
    T1.CODIGO_MOVIMENTACAO,
    T1.VALOR_NOTA, 
    T1.DATA_EMISSAO,
    T1.VALOR_VENCIMENTO1,
    T1.DATA_VENCIMENTO1,
    T1.VALOR_PAGAMENTO1,
    T1.DATA_PAGAMENTO1,
    T1.VALOR_JUROS1,
    T1.VALOR_DESCONTO1,
    T2.NOME_FORNECEDOR,
    ((coalesce(T1.VALOR_VENCIMENTO1,'0') + coalesce(T1.VALOR_VENCIMENTO2,'0') +
      coalesce(T1.VALOR_VENCIMENTO3,'0') + coalesce(T1.VALOR_VENCIMENTO4,'0') + 
      coalesce(T1.VALOR_VENCIMENTO5,'0') + coalesce(T1.VALOR_VENCIMENTO6,'0') +
      coalesce(T1.VALOR_VENCIMENTO7,'0') + coalesce(T1.VALOR_VENCIMENTO8,'0') +
      coalesce(T1.VALOR_VENCIMENTO9,'0') + coalesce(T1.VALOR_VENCIMENTO10,'0') +
      coalesce(T1.VALOR_VENCIMENTO11,'0')+ coalesce(T1.VALOR_VENCIMENTO12,'0')) +
     (coalesce(T1.VALOR_JUROS1,'0') + coalesce(T1.VALOR_JUROS2,'0') +
      coalesce(T1.VALOR_JUROS3,'0') + coalesce(T1.VALOR_JUROS4,'0') +
      coalesce(T1.VALOR_JUROS5,'0') + coalesce(T1.VALOR_JUROS6,'0') +
      coalesce(T1.VALOR_JUROS7,'0') + coalesce(T1.VALOR_JUROS8,'0') +
      coalesce(T1.VALOR_JUROS9,'0') + coalesce(T1.VALOR_JUROS10,'0') +
      coalesce(T1.VALOR_JUROS11,'0')+ coalesce(T1.VALOR_JUROS12,'0'))-
     (coalesce(T1.VALOR_PAGAMENTO1,'0') + coalesce(T1.VALOR_PAGAMENTO2,'0') +
      coalesce(T1.VALOR_PAGAMENTO3,'0') + coalesce(T1.VALOR_PAGAMENTO4,'0') +
      coalesce(T1.VALOR_PAGAMENTO5,'0') + coalesce(T1.VALOR_PAGAMENTO6,'0') +
      coalesce(T1.VALOR_PAGAMENTO7,'0') + coalesce(T1.VALOR_PAGAMENTO8,'0') +
      coalesce(T1.VALOR_PAGAMENTO9,'0') + coalesce(T1.VALOR_PAGAMENTO10,'0') +
      coalesce(T1.VALOR_PAGAMENTO11,'0')+ coalesce(T1.VALOR_PAGAMENTO12,'0'))-
     (coalesce(T1.VALOR_DESCONTO1,'0') + coalesce(T1.VALOR_DESCONTO2,'0') +
      coalesce(T1.VALOR_DESCONTO2,'0') + coalesce(T1.VALOR_DESCONTO4,'0') +
      coalesce(T1.VALOR_DESCONTO5,'0') + coalesce(T1.VALOR_DESCONTO6,'0') +
      coalesce(T1.VALOR_DESCONTO7,'0') + coalesce(T1.VALOR_DESCONTO8,'0') +
      coalesce(T1.VALOR_DESCONTO9,'0') + coalesce(T1.VALOR_DESCONTO10,'0') +
      coalesce(T1.VALOR_DESCONTO11,'0')+ coalesce(T1.VALOR_DESCONTO12,'0'))) AS SALDO
FROM
     CONTAS_PAGAR T1,
     FORNECEDORES T2
WHERE 
     T1.FORNECEDOR = T2.NOME_FORNECEDOR
AND
     SALDO > 0  
AND 
     SALDO < T1.VALOR_NOTA

só que da o erro e não sei resolver.
GOSTEI 0
Thiago Irrazabal

Thiago Irrazabal

02/01/2014

Boa tarde, já tentou usando a clausula "HAVING" ?
É claro que usando HAVING esse teu select vai ficar muito maior mas, é uma maneira, outra maneira que eu faria é com uma procedure, passando o código da movimentação e lá dentro somando esses campos e retornando ele, abraço.



Att,
Thiago Irrazabal de Oliveira
GOSTEI 0
POSTAR