Jump to content
jafm

devolução de duas Somas e média dessas somas

Recommended Posts

jafm

Boas...

Estou a ter alguma dificuldade na construção de uma query...

select Sum(valor) as Valor, Sum(Qnt) as Qnt from dbo.fnResumo('06-06-2013 00:01','06-06-2013 23:59')
UNION
select Sum(valor) as Valor, Sum(Qnt) as Qnt from dbo.fnResumo('06-05-2013 00:01','06-05-2013 23:59')
UNION
select Sum(valor) as Valor, Sum(Qnt) as Qnt from dbo.fnResumo('06-06-2012 00:01','06-06-2012 23:59')

aqui tudo bem devolve me as somos do valor e da qtd de um dado dia...

queria fazer a média do valor/qnt

que ficou assim,

select Sum(valor) as Valor, Sum(Qnt) as Qnt, Sum(valor/Qnt) as Media from dbo.fnResumo('06-06-2013 00:01','06-06-2013 23:59')
UNION all
select Sum(valor) as Valor, Sum(Qnt) as Qnt, Sum(valor/Qnt) as Media from dbo.fnResumo(,'06-05-2013 00:01','06-05-2013 23:59')
UNION all
select Sum(valor) as Valor, Sum(Qnt) as Qnt, Sum(valor/Qnt) as Media from dbo.fnResumo(,'06-06-2012 00:01','06-06-2012 23:59')

Sei que falta o group by mas colocando o group by altera me os resultados finais... como poderei dar a volta a isto?

Share this post


Link to post
Share on other sites
jafm

Boas, já consegui resolver o problema... Utilizei esta formula

SELECT COALESCE(dividend / NULLIF(divisor,0), 0) FROM sometable

Obrigado

  • Vote 1

Share this post


Link to post
Share on other sites
jafm

Vou voltar abrir este tópico, pois foi necessário obter mais um calculo..

E não estou a conseguir obter com a seguinte formula (Valor/ValorTotal*100)

SELECT SUM(valor) AS Valor, SUM(Qnt) AS Qnt, Valor/Sum(Valor)*100 as Percentagem FROM dbo.fnResumo('06-06-2013 00:01','06-06-2013 23:59')

e volto ao mesmo problema do group by...

Como consigo contronar?

Edited by jafm

Share this post


Link to post
Share on other sites
AntonioMateus

Talvez algo assim

SELECT
SUM(valor) AS Valor,
SUM(Qnt) AS Qnt,
SUM(valor)/(select Sum(Valor) from tabelaComTodosValores ) *100 as Percentagem
FROM dbo.fnResumo('06-06-2013 00:01','06-06-2013 23:59')

Edited by AntonioMateus

Share this post


Link to post
Share on other sites
jafm

Olá,

AntonioMateus, a sua solução ajudou mt... agora não consigo é usar o mecanismo do

COALESCE(dividend / NULLIF(divisor,0), 0)

para previnir a divisao por 0

Share this post


Link to post
Share on other sites
AntonioMateus

Julgo que basta algo do género:

COALESCE(dividend / ISNULL(NULLIF(divisor,0),1)  ,  0)

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

×
×
  • 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.