ORACLE中的表和记录如下,
T1 (ID ,UID,UDO)
    1  ,1 , A
    2  ,2 , A  
    3  ,3 , B
    4  ,1 , C
    5  ,2 , B
    6  ,3 , C
    
ID为流水号,UID为人员编号, UDO为人员操作,人员每个操作会记录一条记录进来,
 现在要统计由操作 A变为操作B的人员有哪些?比如上述的表中UID=2满足条件,请问这个sql该怎么写?

解决方案 »

  1.   

    select max(id) from t1 where cudo='A'
      

  2.   

    select  uid from  t1 a,t1 b
    where a.uid=b.uid
    and a.udo='A'
    and b.udo='B'
    and a.id<b.id
      

  3.   

    select  distinct a.uid 
     from  t1 a,t1 b
    where a.uid=b.uid
    and a.udo='A'
    and b.udo='B'
    and a.id<b.id
      

  4.   

    SELECT *
    FROM (
    SELECT id, uid
    Lag(udo) over (PARTITION BY UID ORDER BY id) AS last_udo,
    udo AS this_udo
    FROM table1) a
    WHERE this_udo='B'
    AND last_udo='A'
      

  5.   

    welcome to QQ group:10885799
      

  6.   

    select count(UID) from t1 where UDO='A' or UDO='B' group by TYPE_ID having count(UID)>1
      

  7.   

    2楼的回答很简洁。7楼的方式存在一个'B'变'A'的可能或者'B'->'C'->'A'这样的可能,不严谨。当然如果楼主业务上不存在由'B'到'A'的转化,那7楼的方式更易于理解些