Result + Grouping Mysql result

Asked

Viewed 72 times

1

I have a tremendous doubt in mounting this query that I am attaching, I have a table and cross with another to know the state where had more sales, where the result is grouped and shows me the total value of the state.

I’m willing to bring the total result grouped by the state and down the 5 major cities.

Knowing that in my table I have the data, request, state, city, value and etc...

The result I currently have is this:

inserir a descrição da imagem aqui

I would like the result to be in this other format:

inserir a descrição da imagem aqui

Is there any way to do this? If someone has some content to study it will be of great help.

NOTE: I am using this query:

SELECT controle_uf.estado, alpha.qtd, alpha.vlr, cidade
  FROM (SELECT relat_form_pag_cartao_ods.UF,
               COUNT(relat_form_pag_cartao_ods.Pedido) AS qtd,
               ROUND(SUM(relat_form_pag_cartao_ods.Valor), 0) AS vlr,
               relat_form_pag_cartao_ods.cidade
          FROM relat_form_pag_cartao_ods
         WHERE relat_form_pag_cartao_ods.Data_do_Pedido BETWEEN
               DATE_ADD(CURDATE(), INTERVAL - 30 DAY) AND CURDATE()
           AND relat_form_pag_cartao_ods.
         Status IN
               ('Aprovado manul', 'Aprovado Automatico', 'Em faturamento')
         GROUP BY relat_form_pag_cartao_ods.uf
         ORDER BY qtd DESC) AS alpha
 RIGHT JOIN controle_uf
    ON controle_uf.uf = alpha.UF
 ORDER BY alpha.vlr DESC LIMIT 2
  • Answer your question bro ?

  • @David I’m using this query:

  • you have to [Edit] your post to add the query

  • 1

    I just changed there in the publication.

  • I honestly couldn’t understand your example. By what you describe the first line should not contain the state totals and the 5 following lines the totals of the 5 cities of this state with the highest value? Because in your example the cities appear with different values in "Result state" and "Result city"?

  • In your subquery what is the point of you putting city in your selection list if you are grouping by UF? Or field names have different meaning than usual?

  • @anonimo 1 - The idea of the query was to bring the result of sales in general by UF ex: SP sold 150k, but in SP has 1k of cities, of the 150k in cash sold in the state of SP, which are the 5 cities that sold the most? Better? The values of my created table are illustrative.

  • @anonimo 2 - I want to group by the total value of the state and by the value of the 5 cities with most sales. (Sorry my lack of knowledge, I’m looking to improve myself.)

  • 1

    Next to get the result you want, you will have to do a UNION, first create a query grouping the values by state, then create another grouping the values by city... in the first query in the city field do not enter anything: select state, sale, value, 'the city, 'Result Status' as tipo_result from [query content] Union select status, sale, value, city, 'Result City' as tipo_result from [query content] sacou?

  • 1

    @Raphaelgodoi, I think I understand your point, thank you very much for the comment.

  • Fala @Viitoroliveira :)

  • In SQL Server, it is very simple. Already in the blessed Mysql you will have problems using the LIMIT in sub-query. So it complicates the way @Raphaelgodoi said.

  • Don’t I get it? He won’t need to use sub-query... what I’m suggesting is creating Union, separating the results from the way he wants them... what would be his suggestion?

Show 8 more comments
No answers

Browser other questions tagged

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