select 姓名, 入园时间 , 接送人,
case 1 when 1 then (select 离园时间 from [table] as T1  where t1姓名 = t2.姓名 and t1.入园时间 > t2入园时间 and t1.标记 = 1) else '' end as 离园时间 ,
case 1 when 1 then (select 接送人 from [table] as T3  where t3姓名 = t2.姓名 and t3.入园时间 > t2入园时间 and t3.标记 = 1) else '' end as 接送人1 
from [table] as t1没有测试!楼主自己试一下吧!

解决方案 »

  1.   

    select 姓名,时间 as 入园时间,接送人 as 接人,
    离园时间=isnull((select top 1 时间 from tb1 where 姓名=tb_in.姓名 and 时间>tb_in.时间 and 标记=1),''),
    送人=isnull((select top 1 接送人 from tb1 where 姓名=tb_in.姓名 and 时间>tb_in.时间 and 标记=1 ),'') 
    from tb1 tb_in  where 标记=0 order by 姓名,时间
      

  2.   

    SELECT a.姓名,
           a.时间 as 入园时间,
           case when a.时间 >= b.时间 then null else b.时间 end as 离园时间,
           b.接送人
    FROM TEST A left outer join TEST B on A.姓名= B.姓名
    WHERE a.标记= 0 and
          b.标记= 1做成视图,在视图上建立索引
      

  3.   

    楼主又把这个问题拿出来了啊
    给2个查询语句
    select a.姓名,a.时间 as 入园时间,a.接送人,离园时间 = (select 时间 from 表 where 姓名=a.姓名 and 时间>=a.时间 and  标记 = '1'),接送人 = (select 接送人 from 表 where 姓名=a.姓名 and 时间>=a.时间 and  标记 = '1') from 表 a where 标记 = '0'select a.姓名,a.时间 as 入园时间,b.时间 as 离园时间,a.接送人,b.接送人 from 表 a 
    left join (select 姓名,时间,接送人 from 表 where 标记='1') b on b.姓名=a.姓名 and b.时间 >= a.时间
    where a.标记 = '0'
      

  4.   

    to NewQger(Q哥)
    大哥,上次给的都不理想,有些速度很慢,无奈又来求助。
      

  5.   

    NewQger(Q哥)的方法可行!不過子查詢中最好添加MIN(),因為离园时间可能有多個
      

  6.   

    to  lcy81(听蝉) 我用的机器今天下午都快崩溃了,查4条5列的记录(表中所有记录)要34秒,我去趟厕所回来还没搞出来,系统资源被耗尽了哈~~
      

  7.   

    select a.姓名 ,a.时间 ,a.接送人,min(b.时间) ,b.接送人 from 
    (select * from dbo.Send where  标记='0') as A left join
    (select * from dbo.Send where  标记='1') as B on a.姓名 =B.姓名 and a.时间<b.时间
    group by  a.姓名 ,a.时间 ,a.接送人,b.接送人
    是不时从事门禁系统的兄弟 我的测试表叫 Send 
      

  8.   

    抱歉,没考虑多次入离的问题,修改
    SELECT 姓名, 时间 AS 入园时间, 接送人,(SELECT TOP 1 时间 FROM 表  WHERE 姓名 = a.姓名 AND 时间 >= a.时间 AND 标记 = '1' ORDER BY 时间) AS 离园时间,(SELECT TOP 1 接送人 FROM 表
             WHERE 姓名 = a.姓名 AND 时间 >= a.时间 AND 标记 = '1' ORDER BY 时间) AS 接送人
    FROM 表 a
    WHERE (标记 = '0')