SQLITE查询欲望结果

时间:2012-10-10 08:27:17

标签: sqlite

我的表格格式为(CUSTOMER)(图1.1)

M_CODE  H_CODE END_TIME_OF_SERVICE ADULT ADULT_F
TKLK    LONE     09:19:16           1     2
TKLK    LONE     09:22:11           4     6
TKLK    LONE     09:32:46           2     7
TKLK    LONE     09:32:46           4     9
TKLK    LONE     10:09:36           1     3

如果我运行(SELECT strftime('%H:%M',结束,' - 9分钟')||' - '|| strftime('%H:%M',结束)AS time_slot FROM end_times)它给出了正确的输出(图1.2)

09:19 - 09:28
09:29 - 09:38
09:39 - 09:48
09:49 - 09:58
09:59 - 10:08
10:09 - 10:18 

你能指导如何映射这两个表,让我们说CUSTOMER表中的记录应该总结成人和成人_f列(图1.2到图1.1)

   ADULT    ADULT_F    TIME_SLOT
      5        8      09:19 - 09:28
      6       16      09:29 - 09:38
      0        0      09:39 - 09:48
      0        0      09:49 - 09:58
      0        0      09:59 - 10:08
      1        3      10:09 - 10:18 

请指导我一个解决方案,谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

首先,我们必须以一种我们可以轻松比较的格式获取开始/结束时间(我们可以在以后合并它们):

SELECT strftime('%H:%M', end, '-9 minutes') AS slot_start,
       strftime('%H:%M', end              ) AS slot_end
FROM end_times

然后我们可以将它连接到CUSTOMER表,同时忽略秒:

SELECT ADULT,
       ADULT_F,
       slot_start,
       slot_end
FROM (SELECT strftime('%H:%M', end, '-9 minutes') AS slot_start,
             strftime('%H:%M', end              ) AS slot_end
      FROM end_times)
     LEFT JOIN CUSTOMER
     ON substr(END_TIME_OF_SERVICE, 1, 5) BETWEEN slot_start AND slot_end

最后,我们按插槽分组以获得每个插槽的总和。我使用total代替sum来获取NULL值的零:

SELECT total(ADULT),
       total(ADULT_F),
       slot_start || ' - ' || slot_end
FROM (SELECT strftime('%H:%M', end, '-9 minutes') AS slot_start,
             strftime('%H:%M', end              ) AS slot_end
      FROM end_times)
     LEFT JOIN CUSTOMER
     ON substr(END_TIME_OF_SERVICE, 1, 5) BETWEEN slot_start AND slot_end
GROUP BY slot_start