sql >> Databáze >  >> RDS >> Mysql

Jak vytvořit pohled, který zobrazuje data za poslední 4 čtvrtletí?

Hmmm. Pokud interpretuji „poslední čtyři čtvrtletí“ jako „poslední čtyři čtvrtletí, která jsou v datech“, pak je zde jedna metoda.

Za prvé, můžete získat čtyři čtvrtiny takto:

select distinct p_year, p_quarter
from production p
order by p_year desc, p_quarter desc
limit 4;

Můžete je vyčíslit pomocí proměnných:

select p_year, p_quarter, (@rn := @rn + 1) as enum
from (select distinct p_year, p_quarter
      from production p
      order by p_year desc, p_quarter desc
      limit 4
     ) p cross join
     (select @rn := 0) params

Potom to můžete použít v dotazu k pivotování dat:

select p.person_id,
       sum(case when seqnum = 1 then num else 0 end) as num_quarter_1,
       sum(case when seqnum = 2 then num else 0 end) as num_quarter_2,
       sum(case when seqnum = 3 then num else 0 end) as num_quarter_3,
       sum(case when seqnum = 4 then num else 0 end) as num_quarter_4
from production p join
     (select p_year, p_quarter, (@rn := @rn + 1) as seqnum
      from (select distinct p_year, p_quarter
            from production p
            order by p_year desc, p_quarter desc
            limit 4
           ) p cross join
           (select @rn := 0) params
     ) yq
     using (p_year, p_quarter)
group by p.person_id;

EDIT:

Pokud definujete čtvrtletí na základě aktuálního čtvrtletí, můžete udělat něco docela podobného:

select p.person_id,
       sum(case when seqnum = 1 then num else 0 end) as num_quarter_1,
       sum(case when seqnum = 2 then num else 0 end) as num_quarter_2,
       sum(case when seqnum = 3 then num else 0 end) as num_quarter_3,
       sum(case when seqnum = 4 then num else 0 end) as num_quarter_4
from production p join
     (select year(curdate()) as p_year, quarter(curdate()) as p_quarter, 1 as seqnum union all
      select year(curdate() - interval 1 quarter) as p_year, month(curdate() - interval 1 quarter) as p_quarter, 2 as seqnum union all
      select year(curdate() - interval 2 quarter) as p_year, month(curdate() - interval 2 quarter) as p_quarter, 3 as seqnum union all
      select year(curdate() - interval 2 quarter) as p_year, month(curdate() - interval 3 quarter) as p_quarter, 4 as seqnum           
     ) yq
     using (p_year, p_quarter)
group by p.person_id;

Existují i ​​jiné přístupy, například:

select person_id,
       sum(case when year(curdate()) = p_year and quarter(curdate()) = p_quarter
                then num else 0
           end) as num_quarter_1,
       sum(case when year(curdate() - interval 1 quarter) = p_year and quarter(curdate() - interval 1 quarter) = p_quarter
                then num else 0
           end) as num_quarter_2,
       sum(case when year(curdate() - interval 2 quarter) = p_year and quarter(curdate() - interval 2 quarter) = p_quarter
                then num else 0
           end) as num_quarter_3,
       sum(case when year(curdate() - interval 3 quarter) = p_year and quarter(curdate() - interval 3 quarter) = p_quarter
                then num else 0
           end) as num_quarter_4
from production p
group by person_id;


  1. Kód chyby:1406. Data jsou pro sloupec příliš dlouhá – MySQL

  2. PostgreSQL získá a uvolní LOCK uvnitř uložené funkce

  3. Jak se ujistit, že v databázi MySQL není při inkrementaci pole žádný spor?

  4. Potřebujete dva indexy na spojovací tabulce HABTM?