我正在努力解决这个问题!我的数据如下表所示,但会有多个用户。请注意,它不仅仅是一个开始/结束时间,介于两者之间。
+-------------------------+--------+---------------------------+
| Date | Name 2 | Access |
+-------------------------+--------+---------------------------+
| 2014-09-29 14:50:03.000 | User1 | Ground Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-30 08:42:33.000 | User1 | Ground Floor Door 2 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-30 08:42:58.000 | User1 | 1st Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-30 12:31:54.000 | User1 | Ground Floor Door 1 (Out) |
+-------------------------+--------+---------------------------+
| 2014-09-30 13:05:43.000 | User1 | Ground Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-30 13:11:32.000 | User1 | Ground Floor Door 3 (Out) |
+-------------------------+--------+---------------------------+
| 2014-09-30 13:55:28.000 | User1 | Ground Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-30 13:55:36.000 | User1 | Ground Floor Door 2 (In) |
+-------------------------+--------+---------------------------+
| 2014-09-01 18:31:21.000 | User1 | Ground Floor Door 4 (Out) |
+-------------------------+--------+---------------------------+
| 2014-10-01 08:31:21.000 | User1 | Ground Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-10-01 08:31:41.000 | User1 | Ground Floor Door 2 (In) |
+-------------------------+--------+---------------------------+
| 2014-10-01 17:31:49.000 | User1 | Ground Floor Door 4 (Out) |
+-------------------------+--------+---------------------------+
| 2014-10-02 08:31:51.000 | User1 | Ground Floor Door 1 (In) |
+-------------------------+--------+---------------------------+
| 2014-10-02 18:41:49.000 | User1 | Ground Floor Door 3 (Out) |
+-------------------------+--------+---------------------------+
我需要获取一系列日期之间每天的最小值和最大值。如果我可以计算出午餐时间有多长(第一次和最后一次滑动之间的差异,在下午12点到下午2点之间),这也很好但不重要,直到我能让第一部分工作。
到目前为止,我已尝试过各种版本的查询,但没有任何乐趣。有人可以帮我吗?获取特定日期的最小值和最大值没有问题,只有当涉及的范围不起作用时才会有效。
select min(Date) as EntryTime, max(Date) as ExitTime
from table
where [Name 2] like '%User1%' and EventTime between '2014-09-30 12:00:00' and '2014-10-05 12:00:00'
group by cast(Date as datetime)
这不会提供所需的结果集。我希望我的查询为我的表返回这样的内容:
+-------------------------+-------------------------+----------+------------+
| EntryTime | ExitTime | Username | Date |
+-------------------------+-------------------------+----------+------------+
| 2014-09-30 08:42:33.000 | 2014-09-01 18:31:21.000 | User1 | 2014-09-01 |
+-------------------------+-------------------------+----------+------------+
| 2014-10-01 08:31:21.000 | 2014-10-01 17:31:49.000 | User1 | 2014-10-01 |
+-------------------------+-------------------------+----------+------------+
| 2014-10-02 08:31:51.000 | 2014-10-02 18:41:49.000 | User1 | 2014-10-02 |
+-------------------------+-------------------------+----------+------------+
答案 0 :(得分:1)
我认为问题出在您的演员身上,请尝试CAST AS DATE
:
select name_2,
min(`date`) as EntryTime,
max(`date`) as ExitTime,
cast(`date` As Date) as YourDate
from table
where UserName like '%User1%'
and EventTime between '2014-09-30 12:00:00' and '2014-10-05 12:00:00'
group by cast(`date` As Date), name_2
答案 1 :(得分:0)
这将为您提供每个用户每天的最小值和最大值
的用户列表select cast(Date as datetime) as Date,Name,min(Date) as EntryTime, max(Date) as ExitTime
from table
where Date between '2014-09-30 12:00:00' and '2014-10-05 12:00:00'
group by cast(Date as datetime),Name
您可能需要将日期字段转换为日期类型
答案 2 :(得分:0)
试试这个,看看你得到了什么
SELECT MIN([Date])AS'EntryTime', MAX([Date])AS'ExportTime', 名称 从测试 在哪里铸造([日期]作为日期)BETWEEN施放(getdate() - 2作为日期)和施放(getdate()作为日期) AND NAME ='用户1' GROUP BY CAST([Date] AS DATE),NAME
在我自己的表上测试它可以正常工作。 enter image description here