不知道这样行不?我在表中添加一个字段用来记录各人的合同序号,然后再查询
create table xxx(name char(8),startd char(10),endd char(10),cx int)
insert into xxx values('a','2007-1-2','2007-2-3',1)
insert into xxx values('a','2008-1-2','2008-2-3',2)
insert into xxx values('a','2009-1-2','2009-2-3',3)
insert into xxx values('b','2007-2-2','2007-2-13',1)
insert into xxx values('b','2008-2-2','2008-3-31',2)
insert into xxx values('b','2009-3-2','2009-4-13',3)
insert into xxx values('c','2007-5-2','2008-5-31',1)
insert into xxx values('c','2009-4-2','2009-7-13',2)
select * from (select name 姓名,
case when cx=1 then startd end as
合同1起始日,case when cx=1 then endd end as
合同1结束日,
(select startd from xxx b where b.name=a.name and cx=2) as 合同2起始日,(select endd from xxx b where b.name=a.name and cx=2) as 合同2结束日,
(select startd from xxx c where c.name=a.name and cx=3) as 合同3起始日,(select endd from xxx c where c.name=a.name and cx=3) as 合同3结束日
from xxx a) x
where 合同1起始日 is not null