0
I’m unable to sum up the qtd_itens_purchase with Count cod_person. The query runs normal, frequencybuys the displayed result is the same as the amount of items that comes from "qtd_itens_purchase". In short, I would like to add all the items and divide by the amount of people, taking the cod_person.
select top 10 cod_pessoa as CodPessoa,
dat_compra as DataCompra,
vlr_compra as ValorCompra,
cod_transacao as CodigoTransacao,
sum(qtd_itens_compra)/count(cod_pessoa)as FrequenciaCompra
from tab_transacao
where dat_compra between '2017-08-01' and '2018-08-01'
group by cod_pessoa,
dat_compra,
vlr_compra,
cod_transacao
I didn’t quite understand, but the
sum
and thecount
will accompany the grouping, ie for the combination codpessoa,datacompra,valor,compra,codtransacao. If you need "general" calculation it will not work in a single query– Ricardo Pontual
@Ricardopunctual is right, both functions accompany the
GROUP BY
, soon his query will not work as you want. WhyTOP 10
?– João Martins
Top 10 was just for testing, I needed to get a result of the sum of all my items, with the total amount of people I have.
– user92218
So the solution I presented below should work correctly. It does not return the value you wanted?
– João Martins