我需要找到如何使用时间间隔查询表。
在事件之后添加记录,例如一个事件:十行。 但是如果同一事件再次发生:添加了10个新行。 所以1个事件:20行。
我想查找id min,max和date / time
多次出现的所有事件我有两张桌子:
First contain : ID, EVENT_TYPE, DATE, STATE
Second contain : ID, EVENT_NUMBER, DATA_OF_EVENT
如果事件发生2次,我想要两个结果行
示例数据:
-----------
| TABLE1 |
-----------
1, T1, 20141031 10:00:00
2, T1, 20141031 10:00:00
3, T1, 20141031 10:00:00
4, T1, 20141031 10:00:00
5, T1, 20141031 10:00:00
6, T1, 20141031 10:00:00
7, T1, 20141031 10:00:00
8, T1, 20141031 10:00:00
9, T1, 20141031 10:00:00
10, T1, 20141031 10:20:00
11, T1, 20141031 10:20:00
12, T1, 20141031 10:20:00
13, T1, 20141031 10:20:00
14, T1, 20141031 10:20:00
15, T1, 20141031 10:20:00
16, T2, 20141101 09:10:00
17, T1, 20141101 15:52:00
-----------
| TABLE2 |
-----------
1, EVENT1, Some data...
2, EVENT1, Some data...
3, EVENT1, Some data...
4, EVENT1, Some data...
5, EVENT1, Some data...
6, EVENT1, Some data...
7, EVENT1, Some data...
8, EVENT1, Some data...
9, EVENT1, Some data...
10, EVENT1, Some data...
11, EVENT1, Some data...
12, EVENT1, Some data...
13, EVENT1, Some data...
14, EVENT1, Some data...
15, EVENT1, Some data...
16, EVENT2, Some data...
17, EVENT3, Some data...
------------------
| Wanted result |
------------------
EVENT1, 1, 9, 20141031 10:00:00
EVENT1, 10, 15, 20141031 10:20:00
答案 0 :(得分:0)
编辑新请求:
select a.event_number, b.min_id, b.max_id, cast(b.date as smalldatetime)
from table2 a inner join
(select min(id) min_id, max(id) max_id, cast(date as smalldatetime) date, event_type
from table1
group by cast(date as smalldatetime), event_type) b
on a.id=b.min_id
来自MSDN
ss是两位数,范围从00到59,代表第二个数字。 29.998秒或更短的值向下舍入到最近 分钟,29.999秒或更高的值向上舍入到最近 分钟。