从同一个MySQL表中获取行添加/删除

时间:2017-04-23 22:24:49

标签: mysql sql

我有以下员工表,我想知道根据下面显示的输出,根据“月”,“季度”和“年份”离开/加入公司的人员。

根据数据,我们可以看到Olivia于2017年第一季度离开公司,Geogia和Grace于2017年第二季度加入公司

员工数据库表

| Month | Quarter | Year | Company | Company Description | Employee ID | Employee Name | Country Description |    
| M1    | Q1      | 17   | 020     | US                  | 03237       | Jessica       | UNITED STATES       |
| M1    | Q1      | 17   | 020     | US                  | 05153       | Olivia        | UNITED STATES       |    
| M1    | Q1      | 17   | 020     | US                  | 06809       | Hannah        | UNITED STATES       |    
| M1    | Q2      | 17   | 020     | US                  | 03237       | Jessica       | UNITED STATES       |    
| M1    | Q2      | 17   | 020     | US                  | 0680        | Hannah        | UNITED STATES       |    
| M1    | Q2      | 17   | 020     | US                  | 08434       | Georgia       | UNITED STATES       |    
| M1    | Q2      | 17   | 020     | US                  | 08858       | Grace         | UNITED STATES       |

所需输出

| Month | Quarter | Year | Company | Company Description | Employee ID | Employee Name | Country Description | Status |    
| M1    | Q1      |   17 |     020 | US                  |       03237 | Olivia        | UNITED STATES       | Out    |    
| M1    | Q2      |   17 |     020 | US                  |       08434 | Georgia       | UNITED STATES       | In     |    
| M1    | Q2      |   17 |     020 | US                  |       08858 | Grace         | UNITED STATES       | In     |

我正在尝试使用以下语句,但我被卡住了

SELECT  t.month,
        t.quarter,
        t.year,
        t.employee_id,
       t.employee_name

FROM q4_16_hc AS t
JOIN q4_16_hc AS t1
  ON t1.employee_id = t.employee_id
WHERE t.month <> t1.month
group by t.employee_id

2 个答案:

答案 0 :(得分:0)

您通常会使用full join执行此操作。由于MySQL不支持它,您应该使用union allleft join进行right join

--Employees who left from previous quarter
SELECT t1.month,
    t1.quarter,
    t1.year,
    t1.employee_id,
    t1.employee_name,
    'Out' as status
FROM q4_16_hc AS t1
LEFT JOIN q4_16_hc AS t2 ON t1.employee_id = t2.employee_id AND t1.year = t2.year 
AND t1.Company=t2.Company AND t1.quarter='Q1' AND t2.quarter = 'Q2'
WHERE t2.employee_id is null 
UNION ALL
--Employees who newly joined 
SELECT t2.month,
    t2.quarter,
    t2.year,
    t2.employee_id,
    t2.employee_name,
    'In'
FROM q4_16_hc AS t1
RIGHT JOIN q4_16_hc AS t2 ON t1.employee_id = t2.employee_id AND t1.year = t2.year 
AND t1.Company=t2.Company AND t1.quarter='Q1' AND t2.quarter = 'Q2'
WHERE t1.employee_id is null 

答案 1 :(得分:0)

这非常接近你想要的:

select x.*
from ((select t.employee_id, t.employee_name,
              min(concat_ws(':', year, quarter, month)) as yqm,
              'in' as which
       from q4_16_hc t
       group by t.employee_id, t.employee_name
      ) union all
      (select t.employee_id, t.employee_name,
              max(concat_ws(':', year, quarter, month)) as yqm,
              'out' as which
       from q4_16_hc t
       group by t.employee_id, t.employee_name
      )
     ) x;

如果您真的需要在不同的列中,可以使用substring_index()substr()来提取年份,季度和月份。