我是在SQL2000运行的。比如表A如下:
姓名       流程
李连杰     起床
李连杰     洗脸
李连杰     吃早饭
李连杰     上班胡锦涛     起床
胡锦涛     洗脸
胡锦涛     上班我要通过表A查询出 上班 前的一个流程。如得到结果如下:李连杰     吃早饭
胡锦涛     洗脸要求在SQL2000运行的。

解决方案 »

  1.   

    select id=identity(int,1,1),* into # from [Table]select 姓名,流程=(select 流程 from # where id=a.id-1)
    from # a where 流程='上班'
      

  2.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([姓名] varchar(6),[流程] varchar(6))
    go
    insert [tb]
    select '李连杰','起床' union all
    select '李连杰','洗脸' union all
    select '李连杰','吃早饭' union all
    select '李连杰','上班' union all
    select '胡锦涛','起床' union all
    select '胡锦涛','洗脸' union all
    select '胡锦涛','上班'select id=identity(int,1,1),* into #temp from [tb]select [姓名],[流程]
    from #temp t
    where id = (select id - 1 from #temp where [姓名]=t.[姓名] and [流程]='上班')drop table #temp
    --------------------------
    李连杰 吃早饭
    胡锦涛 洗脸
      

  3.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([姓名] varchar(6),[流程] varchar(6))
    go
    insert [tb]
    select '李连杰','起床' union all
    select '李连杰','洗脸' union all
    select '李连杰','吃早饭' union all
    select '李连杰','上班' union all
    select '胡锦涛','起床' union all
    select '胡锦涛','洗脸' union all
    select '胡锦涛','上班'select id=identity(int,1,1),* into #temp from [tb]select 姓名,流程=(select 流程 from #temp  where id=a.id-1)
    from #temp  a where 流程='上班'drop table #temp
    --------------------------
    李连杰    吃早饭
    胡锦涛    洗脸
      

  4.   

    你这个需要个序号字段,针对每个姓名有唯一的序号才行.
    create table [tb](id int ,[姓名] varchar(6),[流程] varchar(6))
    go
    insert [tb]
    select 1, '李连杰','起床' union all
    select 2,'李连杰','洗脸' union all
    select 3,'李连杰','吃早饭' union all
    select 4,'李连杰','上班' union all
    select 5,'胡锦涛','起床' union all
    select 6,'胡锦涛','洗脸' union all
    select 7,'胡锦涛','上班'
    goselect t.* from tb t where id in (select max(id) from tb m where 姓名 = t.姓名 and id < (select id from tb where 姓名 = m.姓名 and 流程 = '上班')) drop table tb/*
    id          姓名     流程     
    ----------- ------ ------ 
    3           李连杰    吃早饭
    6           胡锦涛    洗脸(所影响的行数为 2 行)
    */否则采用上面的临时表的方法.