如何创建显示最近4季度数据的视图?

时间:2018-04-14 12:13:22

标签: mysql sql

我的环境是5.1.72-community。

我有一个名为production的表,如:

'

到'2018-04-14'时,我们知道quarter = 2,而year = 2018。我将quarter_1定义为当前当前季度,而quarter_2定义为上一季度,等等。现在我想创建一个类似的视图:

 id, person_id, num, p_year, p_quarter
 1, 1, 43, 2018, 2 
 2, 1, 92, 2018, 1
 3, 1, 108, 2017, 4
 4, 2, 41, 2018, 2
 ...

因此,如果该季度没有数字,请填0。

如何创建该视图?

2 个答案:

答案 0 :(得分:0)

嗯。如果我将“过去四个季度”解释为“数据中的最后四个季度”,那么这是一种方法。

首先,您可以通过以下方式获得四个季度:

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

您可以使用变量枚举它们:

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

然后,您可以在查询中使用它来转动数据:

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;

编辑:

如果您根据当前季度定义季度,则可以执行类似的操作:

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;

还有其他方法,例如:

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 :(得分:0)

SELECT
    person_id,
    SUM((p_quarter = 1) * num) as num_quarter_1,
    SUM((p_quarter = 2) * num) as num_quarter_2,
    SUM((p_quarter = 3) * num) as num_quarter_3,
    SUM((p_quarter = 4) * num) as num_quarter_4
FROM production
WHERE p_year = 2018
GROUP BY person_id;