关于时代

时间:2014-08-13 20:14:42

标签: sql count

我有结果集

ID  Datetime                MID EID     Serial     ELapsed_time
0   2014-08-11 19:56:20.923 8A  6147    70          123
0   2014-08-12 00:17:06.090 8A  6147    71          261
0   2014-08-12 01:11:25.833 8A  6368    72           54
0   2014-08-12 02:36:40.710 8A  6147    73           85
0   2014-08-12 09:09:15.400 8A  0861    74          393
0   2014-08-12 11:27:21.870 8A  0861    75          138
0   2014-08-12 16:32:10.680 8A  0861    76          305
0   2014-08-12 20:37:16.767 8A  4273    78          245
0   2014-08-12 22:39:49.740 8A  4273    79          122

我的要求是使用Starttime,EndTime,EIDCOUNT

填充表格

例如,

新表的开始时间应为2014-08-11 19:56:20.923,结束时间应为2014-08-12 00:17:06.090 和EIDCOUNT是1

和下一行,starttime应为2014-08-12 00:17:06.090,结束时间为2014-08-12 01:11:25.833 和EIDCOUNT是1

和下一行,startime应该是2014-08-12 01:11:25.833,endtime是2014-08-12 02:36:40.710 和EIDCOUNT是1

和下一行,starttime应该是2014-08-12 02:36:40.710,endtime是2014-08-12 16:32:10.680 和EIDCOUNT是3

和下一行,starttime应为2014-08-12 16:32:10.680,endtime为2014-08-12 22:39:49.740,EIDCOUNT为2


StartTime                       EndTime              EIDCOUNT
2014-08-11 19:56:20.923    2014-08-12 00:17:06.090         1
2014-08-12 00:17:06.090    2014-08-12 01:11:25.833         1
2014-08-12 01:11:25.833    2014-08-12 02:36:40.710         1
2014-08-12 02:36:40.710    2014-08-12 16:32:10.680         3
2014-08-12 16:32:10.680    2014-08-12 22:39:49.740         2

请帮忙。

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这样的东西:

INSERT INTO TargetTable(StartTime, EndTime, EIDCOUNT)
SELECT MIN(DateTime), MAX(DateTime), COUNT(*)
FROM SourceTable
GROUP BY EID

您的源数据只有四个不同的EID值(0861,4273,6147,6368),因此看起来您的EIDCOUNT应为3,2,3和1。