0
我想建立一个查询来获得特定的输出。这里是我的要求:多个case语句不能正常工作
我有两个表,其中一个是生物识别机指纹打卡细节提取物:
我已经命名为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表的提取物: 即,i已经创建压延表如下:
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。
请在这方面帮助我。 感谢你。
emp_id 111也在2014-01-01 16:00:00打了。因此,'01'列的CASE语句也会返回FD。由于MAX在CASE语句上完成,FD被返回(而不是1H)。 –