Mysql选择具有不同条件的相同列到同一行

时间:2015-07-22 02:31:58

标签: mysql database

我有一个场景需要将行数据累积到结果行的单行中,条件为:

WorkIn = the earliest time from the a day when datetime captured.
LunchIn = the earliest time to lunch hour between 11:50:00 to 13:00:00
LunchOut = time latest time from lunch hour before 13:10:00
WorkOut = time captured the latest after 18:00:00

我有一组数据如下:

id       timer
20605    2015-06-16 07:53:00
20605    2015-06-16 12:00:00
20605    2015-06-16 13:06:00
20605    2015-06-16 18:59:00
20605    2015-06-17 07:52:00
20605    2015-06-17 11:52:00
20605    2015-06-17 12:58:00
20605    2015-06-17 18:26:00
20605    2015-06-18 07:56:00
20605    2015-06-18 11:56:00
20605    2015-06-18 12:56:00
20605    2015-06-18 19:18:00
20211    2015-06-16 07:50:00
20211    2015-06-16 12:05:00
20211    2015-06-16 12:59:00
20211    2015-06-16 18:30:00
20211    2015-06-16 18:59:00
20211    2015-06-16 22:02:00
20211    2015-06-17 07:54:00
20211    2015-06-17 11:56:00
20211    2015-06-17 12:59:00
20211    2015-06-17 12:59:00
20211    2015-06-17 18:28:00
20211    2015-06-17 18:28:00
20211    2015-06-17 19:00:00
20211    2015-06-17 22:20:00
20211    2015-06-18 07:41:00
20211    2015-06-18 11:56:00
20211    2015-06-18 12:55:00
20211    2015-06-18 15:03:00

我想产生一个结果:

id      clockIn               LunchIn               LunchOut              ClockOut
20605    2015-06-16 07:53:00    2015-06-16 12:00:00    2015-06-16 13:06:00    2015-06-16 18:59:00
20605    2015-06-17 07:52:00    2015-06-17 11:52:00    2015-06-17 12:58:00    2015-06-17 18:26:00
20605    2015-06-18 07:56:00    2015-06-18 11:56:00    2015-06-18 12:56:00    2015-06-18 19:18:00
20211    2015-06-16 07:50:00    2015-06-16 12:05:00    2015-06-16 12:59:00    2015-06-16 18:30:00
20211    2015-06-17 07:54:00    2015-06-17 11:56:00    2015-06-17 12:59:00    2015-06-17 12:59:00
20211    2015-06-17 18:28:00    2015-06-17 18:28:00    2015-06-17 19:00:00    2015-06-17 22:20:00
20211    2015-06-18 07:41:00    2015-06-18 11:56:00    2015-06-18 12:55:00    2015-06-18 15:03:00

我尝试过联合,多选,但似乎没有产生我想要的结果。 有人可以帮忙吗?

0 个答案:

没有答案