Row results in columns ( SUM )

Asked

Viewed 106 times

0

Good Afternoon, I have a big question in my query, I have a table with the following info:

inserir a descrição da imagem aqui

I would like the following result

inserir a descrição da imagem aqui

I need the result to be the total value of (forms of payments) per day, divided by period into two columns, M and N.

I believe it is a very simple query, but I’m breaking my head and I’m not getting it.. I’ve tried to use all the GROUP BY.

I found some hint talking to use the PIVOT but I couldn’t do the SUM like this tool.

I got to this query, but it’s not what I want, it brings me the total of the day, not the total of the period as I want

My Query:

SELECT data, (SELECT format(SUM(valor),2,'de_DE') WHERE periodo = 'N') AS N, (SELECT format(SUM(valor),2,'de_DE') WHERE periodo = 'M') AS M FROM floja1 WHERE extract(month FROM data) = '05' GROUP BY data ORDER BY data

Thanks in advance.

Att. Danilo

1 answer

0


Good evening Danillo. Try the following query:

select
    data,
    sum(if(periodo = 'M', valor, 0)) as 'M',
    sum(if(periodo = 'N', valor, 0)) as 'N'
from tabela
group by data
  • Thank you so much! That’s what I wanted!

Browser other questions tagged

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