A表
ID     Name     Action   ActionTime
1       张三     1         2013-8-4
2       张三     1         2013-8-5
3       张三     2        2013-8-6
4       李四     1        2013-8-5
5       李四     2        2013-8-6
如果ACTION没有变化的时候出现重复ACTION=1的取ID小得那条记录 也就是ID=2的那条要过滤掉。求解决办法

解决方案 »

  1.   


    create table A表
    (ID int, Name varchar(10), Action int, ActionTime varchar(16))insert into A表
     select 1, '张三', 1, '2013-8-4' union all
     select 2, '张三', 1, '2013-8-5' union all
     select 3, '张三', 2, '2013-8-6' union all
     select 4, '李四', 1, '2013-8-5' union all
     select 5, '李四', 2, '2013-8-6'
    select * 
     from A表 a
     where a.Action<>1 or (a.Action=1 and 
     not exists(select 1 from A表 b
                where b.name=a.name and b.ID<a.ID))/*
    ID          Name       Action      ActionTime
    ----------- ---------- ----------- ----------------
    1           张三         1           2013-8-4
    3           张三         2           2013-8-6
    4           李四         1           2013-8-5
    5           李四         2           2013-8-6(4 row(s) affected)
    */
      

  2.   

    TO:ap0405140
    这个不对 如果张三ACTION=2后后面又有ACTION=1的记录应该显示出来,也就是当ACTION=2后 重新计算后面的
      

  3.   

    DECLARE @table TABLE(id INT, NAME NVARCHAR(10), Action1 INT, ActionTime DATE)
    INSERT @table
    select '1', '张三', '1', '2013-8-4' union all
    select '2', '张三', '1', '2013-8-5' union all
    select '3', '张三', '2', ' 2013-8-6' union all
    select '4', '李四', '1', ' 2013-8-5' union all
    select '5', '李四', '2', ' 2013-8-6' UNION ALL
    select '8', '李四', '2', ' 2013-8-7' --再加一条测试记录;WITH cte AS
    (
    SELECT num = ROW_NUMBER() OVER(ORDER BY id) -ROW_NUMBER() OVER(PARTITION BY Action1 ORDER BY id), * --关键所在,自己理解一下意思
    FROM @table
    )
    SELECT a.id,a.name,a.action1,a.ActionTime
    FROM cte a
    WHERE NOT EXISTS
    (
    SELECT 1
    FROM cte b
    WHERE b.Action1 = a.Action1
    AND b.num = a.num
    AND b.ActionTime < a.ActionTime
    )
    ORDER BY id
    /*
    id name action1 ActionTime
    1 张三 1 2013-08-04
    3 张三 2 2013-08-06
    4 李四 1 2013-08-05
    5 李四 2 2013-08-06
    */
      

  4.   


    create table A表
    (ID int, Name varchar(10), Action int, ActionTime varchar(16))
     
    insert into A表
     select 1, '张三', 1, '2013-8-4' union all
     select 2, '张三', 1, '2013-8-5' union all
     select 3, '张三', 2, '2013-8-6' union all
     select 4, '张三', 1, '2013-8-6' union all  --> ACTION=2后后面又有ACTION=1的记录
     select 5, '李四', 1, '2013-8-5' union all
     select 6, '李四', 2, '2013-8-6'
    select a.*
     from A表 a
     left join A表 b on a.name=b.name and a.ID=b.ID+1
     where b.ID is null or a.action<>b.action
     
    /*
    ID          Name       Action      ActionTime
    ----------- ---------- ----------- ----------------
    1           张三         1           2013-8-4
    3           张三         2           2013-8-6
    4           张三         1           2013-8-6
    5           李四         1           2013-8-5
    6           李四         2           2013-8-6(5 row(s) affected)
    */