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没有测试!楼主自己试一下吧!
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没有测试!楼主自己试一下吧!
离园时间=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 姓名,时间
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做成视图,在视图上建立索引
给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'
大哥,上次给的都不理想,有些速度很慢,无奈又来求助。
(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
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')