我有一个表,通过MS Access 2013中的表单填充其条目。我附上了下表中部分表的图像。使用StartTime条目,我创建了一个查询,显示在8AM - 9AM之间发生的所有事务,以及另一个查询,以显示在上午9点到10点之间发生的所有事务,依此类推。问题是我有很多疑问。有没有办法将所有查询中的语句组合成一个sql语句,并将结果输出到一个数据表中,并带有列标题' 8 AM-9AM',' 9 AM-10AM',&# 39; 10 AM-11AM'等等。谢谢。
查询示例:
SELECT TimeValue([StartTime])
FROM Table1
WHERE (TimeValue([StartTime]) Between '8:00:00 AM' And '8:59:59 AM')
答案 0 :(得分:0)
这就是我所做的,可以帮助你或者至少让你开始。我为您想要输出的每小时创建了查询(8-9,9-10,10-11等)。然后我创建了一个最终查询,使用UNION
进行所有查询。
您可能已经拥有的查询示例:
SELECT TIMEVALUE(T1.StartTime)
FROM Table1 AS T1
WHERE (TIMEVALUE(T1.StartTime) Between '8:00:00 AM' And '8:59:59 AM');
第二
SELECT TIMEVALUE(T1.StartTime)
FROM Table1 AS T1
WHERE (TIMEVALUE(T1.StartTime) Between '9:00:00 AM' And '9:59:59 AM');
等等......
最终查询的实例:
SELECT '' AS [8-9],'' AS [9-10],'' AS [10-11]
FROM [8-9AM]
UNION
SELECT T1.*,'',''
FROM [8-9AM] AS T1
UNION
SELECT '',T2.*,''
FROM [9-10AM] AS T2
UNION ALL SELECT '','',T3.*
FROM [10-11AM] AS T3
(and so on...);
注意:我必须创建一个空白的第1行,以便正确命名我机器上的列。不确定你是否会遇到同样的问题。
编辑:可能只使用一个表格的简单方法:
SELECT ID,
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '8:00:00 AM' And '8:59:59 AM',
TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '8:00:00 AM' And '8:59:59 AM', ''
) AS [8-9],
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '9:00:00 AM' And '9:59:59 AM', TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '9:00:00 AM' And '9:59:59 AM', ''
) AS [9-10],
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '10:00:00 AM' And '10:59:59 AM',
TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '10:00:00 AM' And '10:59:59 AM', ''
) AS [10-11],
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '11:00:00 AM' And '11:59:59 AM', TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '11:00:00 AM' And '11:59:59 AM', ''
) AS [11-12],
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '12:00:00 AM' And '12:59:59 AM',
TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '12:00:00 AM' And '12:59:59 AM', ''
) AS [12-1],
SWITCH(
TIMEVALUE(T1.StartTime) BETWEEN '1:00:00 AM' And '1:59:59 AM', TIMEVALUE(T1.StartTime)
,TIMEVALUE(T1.StartTime) NOT BETWEEN '1:00:00 AM' And '1:59:59 AM', ''
) AS [1-2]
FROM Table1 AS T1