表结构如下:
online user time
0 f1 t1
1 f1 t2
0 f2 t3
1 f2 t4
0 f1 t5
0 f1 t6
1 f1 t7
1 f2 t8
0 f2 t9
1 f2 t10
1 f3 t11
0 f3 t12
1 f1 t13
1 f4 t14
1 f4 t15
0 f4 t16
1 f4 t17说明:表按时间顺序排列,online中0表示上线,1表示下线,time是时间字段。对每个user,正常情况下是先记录上线后记录下线,表中有坏数据是只记录了上线而没有记录下线,或只记录了下线而没有上线。
现在的问题是如何选出正确的上线下线记录(不能删除原表数据)?
如上表,正确选择结果应为: 0 f1 t1
1 f1 t2
0 f2 t3
1 f2 t4
0 f1 t6
1 f1 t7
0 f2 t9
1 f2 t10
0 f4 t16
1 f4 t17
online user time
0 f1 t1
1 f1 t2
0 f2 t3
1 f2 t4
0 f1 t5
0 f1 t6
1 f1 t7
1 f2 t8
0 f2 t9
1 f2 t10
1 f3 t11
0 f3 t12
1 f1 t13
1 f4 t14
1 f4 t15
0 f4 t16
1 f4 t17说明:表按时间顺序排列,online中0表示上线,1表示下线,time是时间字段。对每个user,正常情况下是先记录上线后记录下线,表中有坏数据是只记录了上线而没有记录下线,或只记录了下线而没有上线。
现在的问题是如何选出正确的上线下线记录(不能删除原表数据)?
如上表,正确选择结果应为: 0 f1 t1
1 f1 t2
0 f2 t3
1 f2 t4
0 f1 t6
1 f1 t7
0 f2 t9
1 f2 t10
0 f4 t16
1 f4 t17
解决方案 »
- 请讲解下这个sql语句。
- 如何查出一张表所关联其他表?
- 我刚学bi,在设计和使用ETL时出现错误,操作性问题
- 救命啊,sql数据库损坏,大家帮帮忙
- SQL2005 查询分析器sql server management studio 如何查找系统函数
- 关于 update
- 20000101转换成 2000-01-01的函数,及 2000-01-012转换成0000101的函数
- 表中有IDENTITY字段,如何进行发布订阅?
- 50分求:一个Oracle语句转化为MS SQL语句
- 高分!在线等待,sql server占用cpu很大资源(95%以上),导致系统暂时没响应
- 在一张表中有价格字段,类型该设置成什么??是real 还是 float 还是别的 急!~~~~~~
- 一个将数据分段的显示的问题?
declare @t table (online int,[user] varchar(10),time varchar(10))
insert @t select 0, 'f1', 't01'
union all select 1, 'f1', 't02'
union all select 0, 'f2', 't03'
union all select 1, 'f2', 't04'
union all select 0, 'f1', 't05'
union all select 0, 'f1', 't06'
union all select 1, 'f1', 't07'
union all select 1, 'f2', 't08'
union all select 0, 'f2', 't09'
union all select 1, 'f2' , 't10'
union all select 1, 'f3', 't11'
union all select 0, 'f3' , 't12'
union all select 1, 'f1', 't13'
union all select 1, 'f4' , 't14'
union all select 1, 'f4', 't15'
union all select 0, 'f4' , 't16'
union all select 1, 'f4' , 't17'select * from @t a
where (
online=0
and (select top 1 online
from @t
where [user]=a.[user]
and time>a.time
order by time
)<>0
)
or
(
online=1
and (select top 1 online
from @t
where [user]=a.[user]
and time<a.time
order by time desc
)<>1
)/*
online user time
----------------------------
0 f1 t01
1 f1 t02
0 f2 t03
1 f2 t04
0 f1 t06
1 f1 t07
0 f2 t09
1 f2 t10
0 f4 t16
1 f4 t17
*/
insert ta select 0, 'f1', 't1'
union all select 1, 'f1', 't2'
union all select 0, 'f2', 't3'
union all select 1, 'f2', 't4'
union all select 0, 'f1', 't5'
union all select 0, 'f1', 't6'
union all select 1, 'f1', 't7'
union all select 1, 'f2', 't8'
union all select 0, 'f2', 't9'
union all select 1, 'f2', 't10'
union all select 1, 'f3', 't11'
union all select 0, 'f3', 't12'
union all select 1, 'f1', 't13'
union all select 1, 'f4', 't14'
union all select 1, 'f4', 't15'
union all select 0, 'f4', 't16'
union all select 1, 'f4', 't17'查询:
select * from ta a
where not --如果只是查询这样样加not
exists(select 1 from ta b where time=(select min(time) from ta where time>a.time)
and (online=a.online or online<a.online) and [user]=a.[user])
结果为:
/*
online user time
----------- ---- -----
0 f1 t1
1 f1 t2
0 f2 t3
1 f2 t4
0 f1 t6
1 f1 t7
0 f2 t9
1 f2 t10
0 f3 t12
1 f1 t13
0 f4 t16
1 f4 t17(所影响的行数为 12 行)
*/
不行呀,我测试出来的是:
0 f1 t1
0 f2 t3
1 f2 t4
0 f1 t6
1 f1 t7
1 f1 t13
0 f4 t16
1 f4 t17