第二次求助熟悉存储过程的同行!
TerminalNO DateTime Number
1001 2012-06-27 12:00:00 10
1001 2012-06-27 12:01:00 11
1001 2012-06-28 12:02:00 12
1002 2012-06-28 12:03:00 40
1002 2012-06-29 12:04:00 46
1002 2012-06-30 12:05:00 49
1003 2012-06-29 12:06:00 14
1003 2012-06-29 12:07:00 20
1003 2012-06-30 12:08:00 37
不同时间的数据在不同的表中,例Path20120627,Path20120628
例如:
属于2012-06-28这张表的的数据有:1001,1002
需求如下:
查询出不同TerminalNO的最大时间和最小时间的的Number,然后相减(最大时间和最小时间是由用户输入的,这
里就存在一个问题,如果输入的不是同一天,就要跨表查询)
返回结果集如下:
TerminalNO StartTime EndTime Number
1001 2012-06-27 12:00:00 2012-06-28 12:02:00 2
1002 2012-06-28 12:03:00 2012-06-29 12:05:00 9
1003 2012-06-29 12:06:00 2012-06-30 12:08:00 23
这存储应该怎么写呀。
[解决办法]
Path20120627 union Path20120628
[解决办法]
;with cte as( select * from Path20120627 union all select * from Path20120628)select TerminalNO,a.[datetime] as StartTime,b.[datetime] as EndTime,b.number-a.number as Numberfrom(select * from cte a where not exists(select 1 from cte where TerminalNO=a.TerminalNO and [datetime]<a.[datetime]) as ajoin(select * from cte b where not exists(select 1 from cte where TerminalNO=b.TerminalNO and [datetime]>b.[datetime]) as bon a.TerminalNO=b.TerminalNO