I can’t get average

Asked

Viewed 51 times

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 sumand the count 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

  • @Ricardopunctual is right, both functions accompany the GROUP BY, soon his query will not work as you want. Why TOP 10?

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

  • So the solution I presented below should work correctly. It does not return the value you wanted?

3 answers

1

There is a specific function for SQL averages, AVG:

SELECT      cod_pessoa              AS CodPessoa
        ,   dat_compra              AS DataCompra
        ,   vlr_compra              AS ValorCompra
        ,   cod_transacao           AS CodigoTransacao
        ,   AVG(qtd_itens_compra)   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 needed to add up the amount of items and divide by the amount of person.

  • João, if you read the question I think he doesn’t want the average of "qtd_itens_purchase" directly, I think q needs a partial sum for after the avg, That’s what I understood

  • I put it that way because he asked the question SUM(qtd_itens_compra).

0


In his query you are grouping by purchase amount, which turns out to be wrong, since from what I understand you want the purchase frequency per day. Adjusting your query would look like this:

SELECT cod_pessoa AS CodPessoa,
      dat_compra AS DataCompra,
      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 dat_compra,
          cod_transacao

0

If you want the total sum, divided by count partial (grouped), you can use a subquery (see if performace looks good):

select top 10 cod_pessoa as CodPessoa,
            dat_compra as DataCompra,   
            vlr_compra as ValorCompra,
            cod_transacao as CodigoTransacao,       
            (select sum(qtd_itens_compra) from tab_transacao)/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 
  • It really worked, but the result seems different from what I need. the Frequenciapurchase column generated the following result: Frequencypurchase: 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 485025.000000 It seems wrong, no? Because in qtd_itens_purchase, there are various values.

  • This depends on Count, put before the calculation of the frequency Count to see the value: count(cod_pessoa) contagem, if the value is the same for each grouping, the calculation of the frequency will be right, and the problem may be in the grouping

  • I did the inversion, the result was different from the other: (select Count(qtd_itens_purchase) from tab_transaction)/sum(vlr_purchase)as Frequenciapurchase

  • ah wasn’t an inversion I guess q didn’t explain it well.. rs was just to add this in the query, just to see qto was counting

Browser other questions tagged

You are not signed in. Login or sign up in order to post.