Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Sign in to follow this  
iron

Problema ao ir buscar valor

Recommended Posts

iron

Boas.

Tenho um pequeno problema com a minha querry.

select MovieID, ((OneStar + (TwoStar*2) + (TreeStar*3) + (FourStar*4) + (FiveStar*5))/(OneStar + TwoStar + TreeStar + FourStar + FiveStar)) as average from
(select * from Movie
where OneStar > 0 or TwoStar>0 or FourStar > 0 or TreeStar>0 or FiveStar>0) a

O que tenho é isto.

Agora eu quero ir buscar o average máximo, e o id do MovieID correspondente.

Mas não estou a conseguir.

Desde já obrigado

P.S

Resolvi o problema assim, sei que não é a maneira mais correcta, mas não estou a conseguir de uma maneira mais fácil.

Se alguém conseguir explicar me como fazer de outra maneira agradecia.

select * from Movie
inner join
 (select max(b.average) as maximum from
  (select MovieID, ((OneStar + (TwoStar*2) + (TreeStar*3) + (FourStar*4) + (FiveStar*5))/(OneStar + TwoStar + TreeStar + FourStar + FiveStar)) as average from
   (select * from Movie
 where OneStar > 0 or TwoStar>0 or FourStar > 0 or TreeStar>0 or FiveStar>0
   ) a
  ) b
 ) c
 on c.maximum = ((OneStar + (TwoStar*2) + (TreeStar*3) + (FourStar*4) + (FiveStar*5))/(OneStar + TwoStar + TreeStar + FourStar + FiveStar))

Edited by iron

Cumprimentos,
iron

Share this post


Link to post
Share on other sites
PaisNeto

Boa tarde,

alterei um pouco a tua query,

não testei e nem estive muito tempo a olhar para ela, foi mesmo uma coisa rápida

verifica se funciona

SELECT MovieID, MAX(average) as average
FROM(
 select MovieID, ((OneStar + (TwoStar*2) + (TreeStar*3) + (FourStar*4) + (FiveStar*5))/(OneStar + TwoStar + TreeStar + FourStar + FiveStar)) as average
 from Movie
 where OneStar > 0
   or TwoStar  > 0
   or FourStar > 0
   or TreeStar > 0
   or FiveStar > 0
   )
GROUP BY MovieID

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

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.