Jump to content

Recommended Posts

Posted

Boa Noite, eu tenho a seguinte instrução

SELECT	 Fundos.Ano, Fundos.Mes, SUM(Fundos.Valor) AS Val, SUM(Euromilhoes.Valor) AS Gasto, SUM(Euromilhoes.Premio) AS Pre, Val + Pre - Gasto AS Tot
FROM		 Fundos, Euromilhoes
WHERE	 (Fundos.Ano = 2014)
GROUP BY Fundos.Mes, Fundos.Ano

So que o Resultado que me da é sempre o dobro do que tenho. Por exemplo:

No Fundos.Valor tenho 30 e ele da-me 60

No Euromilhoes.Valor tenho 6 e ele da-me 12

No Euromilhoes.Premio tenho 25 e ele da-me 50

e no Total a mesma coisa, alguem sabe como resolver?

Obrigado

Pedro'O

Posted

Olá,

O problema é que estás a usar duas tabelas e não as estás a relacionar.

A melhor forma de resolveres é usando um INNER JOIN, assim:

-- Substitui os campos ID pelos teus campos da relação, pois não sei quais são 
SELECT   Fundos.Ano, Fundos.Mes, SUM(Fundos.Valor) AS Val, SUM(Euromilhoes.Valor) AS Gasto, SUM(Euromilhoes.Premio) AS Pre, Val + Pre - Gasto AS Tot
FROM             Fundos INNER JOIN Euromilhoes ON Fundos.ID = Euromilhoes.ID
WHERE    (Fundos.Ano = 2014)
GROUP BY Fundos.Mes, Fundos.Ano

Espero ter ajudado,

  • Vote 1

Pedro Martins

Sharing is Knowledge!

http://www.linkedin.com/in/rechousa

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.