如何选择给定时间范围和项目类别之间的记录

时间:2016-05-24 02:31:41

标签: sql vb.net ms-access

我有一个MS Access数据库表“tbltransactions”,其中包含单独的日期和时间列。我需要从表中选择所有数据来检查三个条件

1.记录特定类别。

2.记录我设置的自定义日期范围。

3.记录我设定的自定义时间范围。

我的表结构如下所示

+-------+---------+-------+--------+------------+-------------+------------+
|  id   |  item   | units | Amount | category   |  adddate    |  addtime   |
+-------+---------+-------+--------+------------+-------------+------------+
|  1    |    A    |   1   |  100   |   toys     |  5/23/2016  |  8:05 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  2    |    A    |   1   |  100   |   toys     |  5/23/2016  |  8:45 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  3    |    B    |   1   |  200   |   book     |  5/23/2016  |  9:05 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  4    |    A    |   1   |  100   |    toys    |  5/23/2016  |  9:25 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  5    |    A    |   1   |  100   |   toys     | 5/23/2016  |  11:05 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  6    |    A    |   1   |  100   |   toys     | 5/23/2016  |  1:10 PM    |
+-------+---------+-------+--------+------------+-------------+------------+
|  7    |    A    |   1   |  100   |   toys     |  5/23/2016  |  3:30 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  8    |    A    |   1   |  100   |   toys     |  5/23/2016  |  4:12 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  9    |    A    |   1   |  100   |   toys     |  5/23/2016  |  5:47 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  10   |    A    |   1   |  100   |   toys     |  5/23/2016  |  6:22 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  11   |    B    |   1   |  200   |   book     |  5/24/2016  |  8:15 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  12   |    B    |   1   |  200   |   book     |  5/24/2016  |  9:33 AM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  13   |    A    |   1   |  100   |   toys     |  5/24/2016  |  1:03 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  14   |    B    |   1   |  200   |   book     |  5/24/2016  |  2:22 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  15   |    A    |   1   |  100   |   toys     |  5/24/2016  |  3:10 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  16   |    A    |   1   |  100   |   toys     |  5/24/2016  |  4:07 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  17   |    A    |   1   |  100   |   toys     |  5/24/2016  |  6:15 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  18   |    B    |   1   |  200   |   book     |  5/24/2016  |  6:17 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  19   |    A    |   1   |  100   |   toys     |  5/24/2016  |  6:30 PM   |
+-------+---------+-------+--------+------------+-------------+------------+
|  20   |    B    |   1   |  200   |   book     |  5/24/2016  |  6:42 PM   |
+-------+---------+-------+--------+------------+-------------+------------+

我可以根据类别和具体日期选择记录

sql = "SELECT id, item 
         FROM Table1
        WHERE category ='toys' 
          AND DateValue(adddate) = #'5/23/2016'#

这将返回记录 1,2,4,5,6,7,8,9,10

我还可以根据类别和日期范围选择记录

sql = "SELECT id, item 
         FROM Table1
        WHERE category ='toys' 
          AND DateValue(adddate) > #'5/23/2016'# 
          AND DateValue(adddate) <= #'5/24/2016'# 

这会返回记录 1,2,4,5,6,7,8,9,10,13,15,16,17,19

现在我需要从 5/23/2016 8:00 AM 2016年5月24日下午3:00 获取记录。怎么做?

3 个答案:

答案 0 :(得分:2)

考虑在Jet / ACE ODBC中可用的CDate()函数以及Access.exe UI中连接字符串/日期值:

sql = "SELECT id, item 
         FROM Table1
        WHERE category ='toys' 
          AND CDate(adddate & ' ' & addtime) > CDate('5/23/2016 8:00 AM')
          AND CDate(adddate & ' ' & addtime) <= CDate('5/24/2016 3:00 PM')

或者,使用BETWEEN运算符(包括端点,因此我添加一秒):

sql = "SELECT id, item 
         FROM Table1
        WHERE category ='toys' 
          AND CDate(adddate & ' ' & addtime) 
          BETWEEN CDate('5/23/2016 8:01 AM') AND CDate('5/24/2016 3:00 PM')

答案 1 :(得分:0)

您可以连接adddateaddtime列并转换为datetime类型以用于比较。

以下是MSSQL中的一个示例,其他平台应该类似:

SELECT id, item 
WHERE category ='toys' AND 
CONVERT(DATETIME, adddate + ' ' + addtime) BETWEEN '5/23/2016 8:00 AM' AND '5/24/2016 3:00 PM'

答案 2 :(得分:0)

对于record from 5/23/2016 8:00 AM to 5/24/2016 3:00 PM,您可以将范围分隔为(date = 5/23 and time > 8:00 AM) or (date=5/24 and time < 3:00 PM)

所以你可以使用这个

sql = "SELECT id, item WHERE category ='toys' AND  ((DateValue(adddate) = #'5/23/2016'# AND  TimeValue(addtime) >= #'8:00:00 AM'#) OR (DateValue(adddate) = #'5/24/2016'# AND TimeValue(addtime) <= #'3:00:00 PM'#))"