日期:2014-05-18  浏览次数:20352 次

(100分求解)超难题!查找在某时间段内的匹配记录
说明:表A是用户上下线记录,表B是用户在线期间的动作记录。

表A结构如下:
action                   ip                           time                           user
上线             21.12.34.7       2007-1-12   10:23:12           u1
上线             21.12.33.9       2007-1-12   10:23:17           u2
。。。。
下线             21.12.34.7       2007-1-12   16:20:15           u1
下线             21.12.33.9       2007-1-12   16:20:18           u2
。。。。

表B结构如下:
                time                               srcip               fromid             destip               toid
2007-1-12   11:24:12           21.12.34.7             f1             192.168.0.1           t1
2007-1-12   11:24:23           21.12.33.9             f2             192.168.0.4           t2
。。。。
2007-1-12   11:28:18           192.168.0.1           t1             21.12.34.7             f1
。。。。
2007-1-12   11:29:12           192.168.0.4           t2             21.12.33.9             f2
。。。。

请问高手,如何根据时间和ip找到某帐号在线期间所使用的id?
如上表,账号u1在线期间所使用的id为f1。

------解决方案--------------------
declare @a table(action varchar(10), ip varchar(20), [time] smalldatetime, [user] varchar(10))
insert @a select '上线 ', '21.12.34.7 ', '2007-1-12 10:23:12 ', 'u1 '
union all select '上线 ', '21.12.33.9 ', '2007-1-12 10:23:17 ', 'u2 '
union all select '下线 ', '21.12.34.7 ', '2007-1-12 16:20:15 ', 'u1 '
union all select '下线 ', '21.12.33.9 ', '2007-1-12 16:20:18 ', 'u2 '


declare @b table(time smalldatetime, srcip varchar(20), fromid varchar(20), destip varchar(20), toid varchar(10))
insert @b select '2007-1-12 11:24:12 ', '21.12.34.7 ', 'f1 ', '192.168.0.1 ', 't1 '
union all select '2007-1-12 11:24:23 ', '21.12.33.9 ', 'f2 ', '192.168.0.4 ', 't2 '
union all select '2007-1-12 11:28:18 ', '192.168.0.1 ', 't1 ', '21.12.34.7 ', 'f1 '
union all select '2007-1-12 11:29:12 ', '192.168.0.4 ', 't2 ', '21.12.33.9 ', 'f2 '


select * from @b bb,
(select ip,[user], stime=(select min([time]) from @a where ip=a.ip and [user]=[a].[user] and action= '上线 '),xtime=(select min([time]) from @a where ip=a