多个案例陈述无法正常运作

时间:2014-07-07 13:32:05

标签: mysql

我正在尝试使用查询来获取特定输出。这是我的要求:

我有两张桌子,其中一张是生物识别机指纹打孔细节的摘录:

我有两个名为 raw_test 日历

的表格
mysql> desc raw_test;
+---------------+----------+------+-----+---------+-------+
| Field         | Type     | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| emp_id        | int(4)   | YES  |     | NULL    |       |
| time_loginout | datetime | YES  |     | NULL    |       |
+---------------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc calender;
+---------------+----------+------+-----+---------+-------+
| Field         | Type     | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| time_loginout | datetime | YES  |     | NULL    |       |
+---------------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)

raw_test表包含以下数据:

mysql> select * from raw_test;
+--------+---------------------+
| emp_id | time_loginout       |
+--------+---------------------+
|    111 | 2014-01-01 09:40:00 |
|    111 | 2014-01-01 16:00:00 |
|    222 | 2014-01-01 09:45:00 |
|    222 | 2014-01-01 17:04:00 |
|    333 | 2014-01-01 09:10:00 |
|    333 | 2014-01-01 17:10:00 |
|    444 | 2014-01-01 09:32:00 |
|    444 | 2014-01-01 17:40:00 |
|    555 | 2014-01-01 09:40:00 |
|    555 | 2014-01-01 17:50:00 |
|    111 | 2014-01-02 09:21:00 |
|    111 | 2014-01-02 17:20:00 |
|    222 | 2014-01-02 09:11:00 |
|    222 | 2014-01-02 17:30:00 |
|    333 | 2014-01-02 09:10:00 |
|    333 | 2014-01-02 17:10:00 |
|    444 | 2014-01-02 09:18:00 |
|    444 | 2014-01-02 16:54:00 |
|    555 | 2014-01-02 09:14:00 |
|    555 | 2014-01-02 16:40:00 |
|    111 | 2014-01-03 09:13:00 |
|    111 | 2014-01-03 16:30:00 |
|    222 | 2014-01-03 09:12:00 |
|    222 | 2014-01-03 17:40:00 |
|    333 | 2014-01-03 09:11:00 |
|    333 | 2014-01-03 17:10:00 |
|    444 | 2014-01-03 09:10:00 |
|    444 | 2014-01-03 17:22:00 |
|    555 | 2014-01-03 09:18:00 |
|    555 | 2014-01-03 17:21:00 |
|    111 | 2014-01-04 09:20:00 |
|    111 | 2014-01-04 17:12:00 |
|    222 | 2014-01-04 09:30:00 |
|    222 | 2014-01-04 17:11:00 |
|    333 | 2014-01-04 09:43:00 |
|    333 | 2014-01-04 17:42:00 |
|    444 | 2014-01-04 09:32:00 |
|    444 | 2014-01-04 17:23:00 |
|    555 | 2014-01-04 09:21:00 |
|    555 | 2014-01-04 17:54:00 |
+--------+---------------------+
40 rows in set (0.00 sec)

和日历保存数据如下:

mysql> select * from calender;
+---------------------+
| time_loginout       |
+---------------------+
| 2014-01-01 09:40:00 |
| 2014-01-01 16:00:00 |
| 2014-01-01 09:45:00 |
| 2014-01-01 17:04:00 |
| 2014-01-01 09:10:00 |
| 2014-01-01 17:10:00 |
| 2014-01-01 09:32:00 |
| 2014-01-01 17:40:00 |
| 2014-01-01 09:40:00 |
| 2014-01-01 17:50:00 |
| 2014-01-02 09:21:00 |
| 2014-01-02 17:20:00 |
| 2014-01-02 09:11:00 |
| 2014-01-02 17:30:00 |
| 2014-01-02 09:10:00 |
| 2014-01-02 17:10:00 |
| 2014-01-02 09:18:00 |
| 2014-01-02 16:54:00 |
| 2014-01-02 09:14:00 |
| 2014-01-02 16:40:00 |
| 2014-01-03 09:13:00 |
| 2014-01-03 16:30:00 |
| 2014-01-03 09:12:00 |
| 2014-01-03 17:40:00 |
| 2014-01-03 09:11:00 |
| 2014-01-03 17:10:00 |
| 2014-01-03 09:10:00 |
| 2014-01-03 17:22:00 |
| 2014-01-03 09:18:00 |
| 2014-01-03 17:21:00 |
| 2014-01-04 09:20:00 |
| 2014-01-04 17:12:00 |
| 2014-01-04 09:30:00 |
| 2014-01-04 17:11:00 |
| 2014-01-04 09:43:00 |
| 2014-01-04 17:42:00 |
| 2014-01-04 09:32:00 |
| 2014-01-04 17:23:00 |
| 2014-01-04 09:21:00 |
| 2014-01-04 17:54:00 |
+---------------------+
40 rows in set (0.00 sec

此处日历表是 raw_test 表的摘录: 即,我已按如下方式创建了日历表:

create table calender as (select time_loginout from raw_test);

如果我执行以下查询,则无法获得预期的输出:

select r.emp_id, 
max(case 
when c.time_loginout >= '2014-01-01 09:00:00' and c.time_loginout <= '2014-01-01 09:15:00' then 'P' 
when c.time_loginout > '2014-01-01 09:15:00' and c.time_loginout <= '2014-01-01 09:30:00' then 'L'
when c.time_loginout > '2014-01-01 09:30:00' and c.time_loginout <= '2014-01-01 10:00:00' then '1H'
when c.time_loginout > '2014-01-01 10:00:00' and c.time_loginout <= '2014-01-01 11:00:00' then '2H'
when c.time_loginout > '2014-01-01 11:00:00' and c.time_loginout <= '2014-01-01 12:00:00' then '3H'
when c.time_loginout > '2014-01-01 12:00:00' and c.time_loginout <= '2014-01-01 13:30:00' then 'HD'
when c.time_loginout > '2014-01-01 13:30:00' and c.time_loginout <= '2014-01-01 14:00:00' then 'HD'
when c.time_loginout > '2014-01-01 14:00:00' and c.time_loginout <= '2014-01-01 17:00:00' then 'FD' 
else 'EO' end)'01',
max(case 
when c.time_loginout >= '2014-01-02 09:00:00' and c.time_loginout <= '2014-01-02 09:15:00' then 'P' 
when c.time_loginout > '2014-01-02 09:15:00' and c.time_loginout <= '2014-01-02 09:30:00' then 'L'
when c.time_loginout > '2014-01-02 09:30:00' and c.time_loginout <= '2014-01-02 10:00:00' then '1H'
when c.time_loginout > '2014-01-02 10:00:00' and c.time_loginout <= '2014-01-02 11:00:00' then '2H'
when c.time_loginout > '2014-01-02 11:00:00' and c.time_loginout <= '2014-01-02 12:00:00' then '3H'
when c.time_loginout > '2014-01-02 12:00:00' and c.time_loginout <= '2014-01-02 13:30:00' then 'HD'
when c.time_loginout > '2014-01-02 13:30:00' and c.time_loginout <= '2014-01-02 14:00:00' then 'HD'
when c.time_loginout > '2014-01-02 14:00:00' and c.time_loginout <= '2014-01-02 17:00:00' then 'FD' 
else 'EO' end)'02',
max(case 
when c.time_loginout >= '2014-01-03 09:00:00' and c.time_loginout <= '2014-01-03 09:15:00' then 'P' 
when c.time_loginout > '2014-01-03 09:15:00' and c.time_loginout <= '2014-01-03 09:30:00' then 'L'
when c.time_loginout > '2014-01-03 09:30:00' and c.time_loginout <= '2014-01-03 10:00:00' then '1H'
when c.time_loginout > '2014-01-03 10:00:00' and c.time_loginout <= '2014-01-03 11:00:00' then '2H'
when c.time_loginout > '2014-01-03 11:00:00' and c.time_loginout <= '2014-01-03 12:00:00' then '3H'
when c.time_loginout > '2014-01-03 12:00:00' and c.time_loginout <= '2014-01-03 13:30:00' then 'HD'
when c.time_loginout > '2014-01-03 13:30:00' and c.time_loginout <= '2014-01-03 14:00:00' then 'HD'
when c.time_loginout > '2014-01-03 14:00:00' and c.time_loginout <= '2014-01-03 17:00:00' then 'FD' 
else 'EO' end)'03',
max(case 
when c.time_loginout >= '2014-01-04 09:00:00' and c.time_loginout <= '2014-01-04 09:15:00' then 'P' 
when c.time_loginout > '2014-01-04 09:15:00' and c.time_loginout <= '2014-01-04 09:30:00' then 'L'
when c.time_loginout > '2014-01-04 09:30:00' and c.time_loginout <= '2014-01-04 10:00:00' then '1H'
when c.time_loginout > '2014-01-04 10:00:00' and c.time_loginout <= '2014-01-04 11:00:00' then '2H'
when c.time_loginout > '2014-01-04 11:00:00' and c.time_loginout <= '2014-01-04 12:00:00' then '3H'
when c.time_loginout > '2014-01-04 12:00:00' and c.time_loginout <= '2014-01-04 13:30:00' then 'HD'
when c.time_loginout > '2014-01-04 13:30:00' and c.time_loginout <= '2014-01-04 14:00:00' then 'HD'
when c.time_loginout > '2014-01-04 14:00:00' and c.time_loginout <= '2014-01-04 17:00:00' then 'FD' 
else 'EO' end)'04'
from raw_test r left join calender c on r.time_loginout=c.time_loginout group by r.emp_id;

以上查询的输出:

+--------+------+------+------+------+
| emp_id | 01   | 02   | 03   | 04   |
+--------+------+------+------+------+
|    111 | FD   | L    | P    | L    |
|    222 | EO   | P    | P    | L    |
|    333 | P    | P    | P    | EO   |
|    444 | EO   | L    | P    | EO   |
|    555 | EO   | P    | L    | L    |
+--------+------+------+------+------+
5 rows in set (0.00 sec)

实际上我正在寻找每个emp_id可以是以下之一:

P - present
L - late
EO - eary out
1H - 1 hour late
2H - 2 hour late
3H - 3 hour late
HD - half day late
FD - full day late

但是,emp_id 111在9:40之前打了一拳,并显示了FD,它应该显示为1H。

请在这方面帮助我。 感谢你。

1 个答案:

答案 0 :(得分:0)

如果您正在寻找入住时间,您只希望最小值不是最大值。此外,我认为没有必要你的第二个表日历。在这个例子中,它似乎没有做任何事情。试试这个:

select emp_id, 
MIN(case 
when time_loginout >= '2014-01-01 09:00:00' and time_loginout <= '2014-01-01 09:15:00' then 'P' 
when time_loginout > '2014-01-01 09:15:00' and time_loginout <= '2014-01-01 09:30:00' then 'L'
when time_loginout > '2014-01-01 09:30:00' and time_loginout <= '2014-01-01 10:00:00' then '1H'
when time_loginout > '2014-01-01 10:00:00' and time_loginout <= '2014-01-01 11:00:00' then '2H'
when time_loginout > '2014-01-01 11:00:00' and time_loginout <= '2014-01-01 12:00:00' then '3H'
when time_loginout > '2014-01-01 12:00:00' and time_loginout <= '2014-01-01 13:30:00' then 'HD'
when time_loginout > '2014-01-01 13:30:00' and time_loginout <= '2014-01-01 14:00:00' then 'HD'
when time_loginout > '2014-01-01 14:00:00' and time_loginout <= '2014-01-01 17:00:00' then 'FD' 
else 'EO' end)'01',
MIN(case 
when time_loginout >= '2014-01-02 09:00:00' and time_loginout <= '2014-01-02 09:15:00' then 'P' 
when time_loginout > '2014-01-02 09:15:00' and time_loginout <= '2014-01-02 09:30:00' then 'L'
when time_loginout > '2014-01-02 09:30:00' and time_loginout <= '2014-01-02 10:00:00' then '1H'
when time_loginout > '2014-01-02 10:00:00' and time_loginout <= '2014-01-02 11:00:00' then '2H'
when time_loginout > '2014-01-02 11:00:00' and time_loginout <= '2014-01-02 12:00:00' then '3H'
when time_loginout > '2014-01-02 12:00:00' and time_loginout <= '2014-01-02 13:30:00' then 'HD'
when time_loginout > '2014-01-02 13:30:00' and time_loginout <= '2014-01-02 14:00:00' then 'HD'
when time_loginout > '2014-01-02 14:00:00' and time_loginout <= '2014-01-02 17:00:00' then 'FD' 
else 'EO' end)'02',
MIN(case 
when time_loginout >= '2014-01-03 09:00:00' and time_loginout <= '2014-01-03 09:15:00' then 'P' 
when time_loginout > '2014-01-03 09:15:00' and time_loginout <= '2014-01-03 09:30:00' then 'L'
when time_loginout > '2014-01-03 09:30:00' and time_loginout <= '2014-01-03 10:00:00' then '1H'
when time_loginout > '2014-01-03 10:00:00' and time_loginout <= '2014-01-03 11:00:00' then '2H'
when time_loginout > '2014-01-03 11:00:00' and time_loginout <= '2014-01-03 12:00:00' then '3H'
when time_loginout > '2014-01-03 12:00:00' and time_loginout <= '2014-01-03 13:30:00' then 'HD'
when time_loginout > '2014-01-03 13:30:00' and time_loginout <= '2014-01-03 14:00:00' then 'HD'
when time_loginout > '2014-01-03 14:00:00' and time_loginout <= '2014-01-03 17:00:00' then 'FD' 
else 'EO' end)'03',
MIN(case 
when time_loginout >= '2014-01-04 09:00:00' and time_loginout <= '2014-01-04 09:15:00' then 'P' 
when time_loginout > '2014-01-04 09:15:00' and time_loginout <= '2014-01-04 09:30:00' then 'L'
when time_loginout > '2014-01-04 09:30:00' and time_loginout <= '2014-01-04 10:00:00' then '1H'
when time_loginout > '2014-01-04 10:00:00' and time_loginout <= '2014-01-04 11:00:00' then '2H'
when time_loginout > '2014-01-04 11:00:00' and time_loginout <= '2014-01-04 12:00:00' then '3H'
when time_loginout > '2014-01-04 12:00:00' and time_loginout <= '2014-01-04 13:30:00' then 'HD'
when time_loginout > '2014-01-04 13:30:00' and time_loginout <= '2014-01-04 14:00:00' then 'HD'
when time_loginout > '2014-01-04 14:00:00' and time_loginout <= '2014-01-04 17:00:00' then 'FD' 
else 'EO' end)'04'
from raw_test  group by emp_id;