2014-07-07 35 views
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。

請在這方面幫助我。 感謝你。

+0

emp_id 111也在2014-01-01 16:00:00打了。因此,'01'列的CASE語句也會返回FD。由於MAX在CASE語句上完成,FD被返回(而不是1H)。 –

回答

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;