Assistance with SQL

Asked

Viewed 66 times

2

Guys I have the following problem... I have a table XXX that has the following fields: k803, codrer, periodo, meta, challenge.

inserir a descrição da imagem aqui

The table shows the goal and challenge of each store representative 802 in the period of January 2018.

I had to make the media of this store in this period staying like this: inserir a descrição da imagem aqui

The point is, I need to do this for every month and put all the information in one query just, for example "January goal and challenge media," "goal media and February challenge," ... and so on.

Query average:

SELECT  AVG(XXX.META)       AS META
    ,   AVG(XXX.DESAFIO)    AS DESAFIO 
FROM    XXX 
WHERE   XXX.K803    = '802' 
    AND XXX.periodo = '032018'

Does anyone have any idea if this can be done ?

  • Put pf to query SQL you used to reach the average you indicated in your question. Also avoid placing images of the query or results, put in formatted text that is easier to read (and facilitates the copy/Paste!).

  • Perfect .. thanks for the tips. Follow the Query of the average. SELECT AVG(XXX.META) AS META,AVG(XXX.CHALLENGE) AS CHALLENGE FROM XXX WHERE XXX.K803 = '802' AND XXX.period = '012018'

1 answer

3


I think something like that solves your problem:

SELECT      X.K803
        ,   X.CODREPR
        ,   X.PERIODO
        ,   X2.META
        ,   X2.DESAFIO
FROM        XXX X   
INNER JOIN  (
                SELECT      K803
                        ,   PERIODO
                        ,   AVG(META)       AS META
                        ,   AVG(DESAFIO)    AS DESAFIO
                FROM        XXX
                GROUP BY    K803
                        ,   PERIODO
            )   X2  ON  X2.K803     = X.K803
                    AND X2.PERIODO  = X.PERIODO

Then you can always apply the filters you want.


If you don’t need the column CODREPR then you can do it all at once!

SELECT      K803
        ,   PERIODO
        ,   AVG(META)       AS META
        ,   AVG(DESAFIO)    AS DESAFIO
FROM        XXX
GROUP BY    K803
        ,   PERIODO
WHERE       K803 = '802'

The clause WHERE is optional. If you include it, it will only return the results to the '802' store, if removed it will return to all stores.

  • I’m going to take a look, I’m going to run tests and I warn you later here, at first it didn’t work out that way. But I’ll see what it might be,

  • Actually I won’t need the codrepr, just a question, I put GROUP BY there right after FROM and below that I keep INNER JOIN until the end, correct ?

  • If you don’t need the column CODREPR do not need to keep the INNER JOIN. It looks exactly as it does on my second appointment.

  • Perfect, and in case I want to take only the data of k803 = '802' has as ? Put a WHERE at the end is possible ?

  • Yes, of course :). Answer edited to include filters.

  • Thanks for the help, everything working as expected. Thank you

  • Happy to help! If the answer was helpful then mark it as accepted and give an UP!

Show 3 more comments

Browser other questions tagged

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