表结构如下:
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

解决方案 »

  1.   

    --f3两条数据因为按1,0排列,所以是坏记录
    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
    */
      

  2.   

    对比online列,如果有重复的为1就删掉后一条,为0就删前一条
      

  3.   

    create table ta(online int,    [user]  varchar(2) , time varchar(5))
    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 行)
    */
      

  4.   

    gc_ding(施主,给个妞泡好么):
    不行呀,我测试出来的是:
    0 f1 t1
    0 f2 t3
    1 f2 t4
    0 f1 t6
    1 f1 t7
    1 f1 t13
    0 f4 t16
    1 f4 t17
      

  5.   

    我知道了,和time有关。有正确答案了。谢谢。