求一存储过程(取出时间小于三分钟的前后二条记录,详细见内)在线等(100分)

  • 主题发起人 主题发起人 discovered
  • 开始时间 开始时间
D

discovered

Unregistered / Unconfirmed
GUEST, unregistred user!
数据结构如下<br>id &nbsp; &nbsp; amount &nbsp; createtime<br>1 &nbsp; &nbsp; &nbsp; 200 &nbsp; &nbsp; &nbsp;2006-2-12 16:45:30<br>2 &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp;2006-2-13 14:43:29<br>3 &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp;2006-2-13 14:45:00<br>4 &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; 2006-2-13 14:47:20<br>5 &nbsp; &nbsp; &nbsp; 23 &nbsp; &nbsp; &nbsp; 2006-2-15 19:20:02<br>6 &nbsp; &nbsp; &nbsp; 120 &nbsp; &nbsp; &nbsp;2006-2-17 18:00:00<br>7 &nbsp; &nbsp; &nbsp; 200 &nbsp; &nbsp; &nbsp;2006-2-17 18:02:20<br>8 &nbsp; &nbsp; &nbsp; 100 &nbsp; &nbsp; &nbsp;2006-2-20 19:00:33<br><br>条件为相邻数据的创建时间小于三分钟的数据<br>要取出的数据如下<br>2 &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp;2006-2-13 14:43:29<br>3 &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp;2006-2-13 14:45:00<br>4 &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; 2006-2-13 14:47:20<br>6 &nbsp; &nbsp; &nbsp; 120 &nbsp; &nbsp; &nbsp;2006-2-17 18:00:00<br>7 &nbsp; &nbsp; &nbsp; 200 &nbsp; &nbsp; &nbsp;2006-2-17 18:02:20
 
-- oracle语句:<br><br>-- 建表插数据<br>drop table Table_17;<br><br>create table Table_17 &nbsp;(<br>&nbsp; &nbsp;id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INTEGER &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;not null,<br>&nbsp; &nbsp;amount &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DECIMAL(9,2),<br>&nbsp; &nbsp;createtime &nbsp; &nbsp; &nbsp; &nbsp;DATE,<br>&nbsp; &nbsp;constraint PK_TABLE_17 primary key (id)<br>);<br><br><br>insert into Table_17 values (1,200,to_date('2006-2-12 16:45:30','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (2,500,to_date('2006-2-13 14:43:29','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (3,500,to_date('2006-2-13 14:45:00','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (4,20,to_date('2006-2-13 14:47:20','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (5,23,to_date('2006-2-15 19:20:02','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (6,120,to_date('2006-2-17 18:00:00','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (7,200,to_date('2006-2-17 18:02:20','YYYY-MM-DD HH24:MI:SS'));<br>insert into Table_17 values (8,100,to_date('2006-2-20 19:00:33','YYYY-MM-DD HH24:MI:SS'));<br><br><br>commit;<br><br>-- 语句<br>select e.id,e.amount,e.createtime <br>from <br>(select <br>&nbsp; &nbsp; &nbsp; &nbsp;(select c.createtime <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from Table_17 c <br>&nbsp; &nbsp; &nbsp; &nbsp; where c.createtime=(select max(createtime) from table_17 d where d.createtime&lt;b.createtime)) as aaa,<br>&nbsp; &nbsp; &nbsp; &nbsp;(select c.createtime <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;from Table_17 c <br>&nbsp; &nbsp; &nbsp; &nbsp; where c.createtime=(select min(createtime) from table_17 d where d.createtime&gt;b.createtime)) as bbb,<br>&nbsp; &nbsp; &nbsp; &nbsp;b.* <br>from (select rownum as rid,a.* from Table_17 a) b) e<br>where e.createtime-e.aaa&lt;=1/480 or abs(e.bbb-e.createtime)&lt;=1/480;<br><br>效率先不论,达到效果,最近发现oracle的sql语句很好用。
 
declare @createtime datetime<br>create table #aa(create_time datetime)<br>declare A_cursor cursor for select createtime from table1<br>open A_cursor<br>fetch next from A_cursor into @createtime<br>while (@@fetch_status=0)<br>&nbsp;begin<br>&nbsp; &nbsp;insert into #aa select createtime from table1 where datediff(minute,@createtime,createtime)&lt;3 and createtime&gt;@createtime<br>&nbsp; &nbsp;if (select count(*) from table1 where datediff(minute,@createtime,createtime)&lt;3 and createtime&gt;@createtime)&lt;&gt;0<br>&nbsp; &nbsp; &nbsp; &nbsp;insert into #aa select createtime from table1 where &nbsp;createtime=@createtime<br>&nbsp; &nbsp;fetch next from A_cursor into @createtime<br>end<br>select * from table1 where createtime in (select * from #aa)<br><br>drop table #aa<br>close A_cursor<br>deallocate A_cursor
 
FYI.MSSQL<br><br>Select * from Table_17 where exists <br>(Select 1 from Table_17 t2 <br>where Table_17.createtime between DateAdd(minute, -3, t2.createtime) and DateAdd(minute, 3, t2.createtime) and Table_17.ID &lt;&gt; t2.ID )
 
后退
顶部