Jump to content

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


jafm

Recommended Posts

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?

Link to comment
Share on other sites

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
Link to comment
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.