Group column sum by quarter

Asked

Viewed 1,837 times

4

I would like to group the sum of one column per quarter, that is every three months.

I have the following query which groups from month to month:

select month(data) as mes, year(data) as ano, ifnull(sum(det.quantidade),0) as total
    from documento as doc
    inner join documento_serie as serie on serie.id = doc.documento_serie_id
    inner join documento_detail as det on doc.id = det.documento_id
    inner join phos_iva as iva on iva.id = det.iva_id
    where serie.documento_categoria_id = 3 
    group by mes, ano 
    order by mes, ano desc

How can I group the sum of a column at the intervals of [January, March],[April, June],[July, September],[October, December]?

It should also be months for a single year.

2 answers

1

select CASE WHEN MONTH(data) in (1,2,3) THEN 'Primeiro'
            WHEN MONTH(data) in (4,5,6) THEN 'Segundo'
            WHEN MONTH(data) in (7,8,9) THEN 'Terceiro'
            WHEN MONTH(data) in (10,11,12) THEN 'Quarto'
       END AS trimestre, 
       YEAR(data) as ano, 
       SUM(CASE WHEN MONTH(data) in (1,2,3) THEN det.quantidade 
                WHEN MONTH(data) in (4,5,6) THEN det.quantidade 
                WHEN MONTH(data) in (7,8,9) THEN det.quantidade 
                WHEN MONTH(data) in (10,11,12) THEN det.quantidade 
           END) as total
from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 3 
group by ano, CASE WHEN MONTH(data) in (1,2,3) THEN 1
                   WHEN MONTH(data) in (4,5,6) THEN 2
                   WHEN MONTH(data) in (7,8,9) THEN 3
                   WHEN MONTH(data) in (10,11,12) THEN 4
              END
order by data desc
  • There would be no way to group by MONTH(data-1)/4?

0

The syntax below works on SQL Server, you could easily adapt it to MYSQL, the important here is the concept:

select 

sum(case when month(data) in (1,2,3) then ifnull(det.quantidade,0) else 0 end) Tri1,
sum(case when month(data) in (4,5,6) then ifnull(det.quantidade,0) else 0 end) Tri2,
sum(case when month(data) in (7,8,9) then ifnull(det.quantidade,0) else 0 end) Tri3,
sum(case when month(data) in (10,11,12) then ifnull(det.quantidade,0) else 0 end) Tri4,
year(data) as ano

from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 3 
group by ano 
order by ano desc

Explanation:

sum(case when month(data) in (1,2,3) then ifnull(det.quantidade,0) else 0 end) Tri1

The above code will add the value of det.quantidade when the month is 1,2 or 3, otherwise it will add 0.

This is done for each Quarter. Thus, this SQL will return 5 columns, being 1 for each Quarter and another for the Year.

  • I am really Noob in mysql, but it is giving error. If you do not put the "end" in the 'sum' of quarter 4, it says 'You have an error in sql syntax ..." if you put the end says, 'Invalid use of group Function'.

  • The end was my mistake, I forgot to put. I just edited. About the cluster error, I need to investigate. If you exchange group by year, by group by year(data), does it work? (change the order by also).

  • I think it continues with an extra end in quarter 4, anyway I tested what you told me and gives the same error :

  • The error is no longer syntax, that is, the Mysql engine is parsing SQL without any problem. The error is given for another reason. I don’t work with Mysql, so I need to investigate what might be. Anyway, it’s pretty weird that it doesn’t work.

  • See also http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_quarter

Browser other questions tagged

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