2

我正在尝试对 Mysql 中的表进行转换。我不知道该怎么做。谁能告诉我该怎么做?输入和输出是给定的。我想知道它是怎么做的?

输入表

+-------------+------------+------------------+-------------------+
| Employee_ID | Start_Date | Termination_Date | Performance_Level |
+-------------+------------+------------------+-------------------+
|           1 | 1/1/2007   | 3/1/2007         | Low               |
|           2 | 6/5/2004   | Null             | Medium            |
|           3 | 4/3/2003   | Null             | High              |
|           4 | 9/1/2002   | 4/15/2007        | Medium            |
|           5 | 4/6/2007   | 11/1/2007        | Low               |
|           6 | 7/1/2007   | Null             | High              |
|           7 | 3/2/2005   | 8/1/2007         | Low               |
+-------------+------------+------------------+-------------------+

输出表

+---------+-----------------------------------+-----------------+-------------------+----------------+
| Period  |   Total_Employees_at_end_of_quarter | High_Performers | Medium_Performers | Low_Performers |
+---------+-----------------------------------+-----------------+-------------------+----------------+
| Q1-2007 |                                 4 |               1 |                 2 |              1 |
| Q2-2007 |                                 4 |               1 |                 1 |              2 |
| Q3-2007 |                                 4 |               2 |                 1 |              1 |
| Q4-2007 |                                 3 |               2 |                 1 |              0 |
+---------+-----------------------------------+-----------------+-------------------+----------------+

这是我尝试过的

select * from emp 
where date(sdate)< date'2007-04-01' and (date(tdate)> date'2007-03-31' or tdate is null);

select * from emp 
where date(sdate)< date'2007-07-01' and (date(tdate)> date'2007-06-30' or tdate is null);

select * from emp 
where date(sdate)< date'2007-010-01' and (date(tdate)> date'2007-09-30' or tdate is null);

select * from emp 
where date(sdate)< date'2008-01-01' and (date(tdate)> date'2007-12-31' or tdate is null);

我有单独的查询,但我想要一个可以提供输出的查询。

4

2 回答 2

2

下面采用的方法是为每个季度创建一个驱动因素表,其中包含有关年份和季度的信息。然后使用非等值连接将其连接到员工表。在季度或之前开始并在季度之后结束的员工在季度末处于活跃状态。

它使用一个技巧进行日期比较,即通过将年份乘以 4 并添加季度,将年-季度组合转换为季度计数。这便于简化日期比较。

select driver.qtryr, count(*) as TotalPerformers,
       sum(Performance_level = 'High') as HighPerformers,
       sum(Performance_level = 'Medium') as MediumPerformers,
       sum(Performance_level = 'Low') as LowPerformers
from (select 2007 as yr, 1 as qtr, 'Q1-2007' as qtryr union all
      select 2007 as yr, 2 as qtr, 'Q2-2007' as qtryr union all
      select 2007 as yr, 3 as qtr, 'Q3-2007' as qtryr union all
      select 2007 as yr, 4 as qtr, 'Q4-2007' as qtryr 
     ) driver left outer join
     Table1 emp
     on year(emp.start_date)*4+quarter(emp.start_date) <= driver.yr*4+qtr and
        (emp.termination_date is null or
         year(emp.termination_date)*4+quarter(emp.termination_date) > driver.yr*4+qtr
        )
group by driver.qtryr
于 2013-06-22T00:17:12.023 回答
0

尝试这个

SELECT QUARTER('2008-04-01');

http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_quarter

和 CONCAT()

于 2013-06-22T00:11:02.743 回答