请教高手:此数据库为本人设计的新农保和城居保的数据库的一部分
一对多的两个表(相当于一个台账一个流水账),建立查询,如何按缴费时间只显示最近的一条记录?(以便作为下年应征的数据)。
我用以下语句查询出来有重复(不应有重复)
SELECT a.*
FROM 05个人明细 AS a
WHERE exists(select 1 from 05个人明细 where 公民身份证=a.公民身份证
and a.缴费类型 = '正常应缴'
and 时间<a.时间);
SELECT c.*
FROM 04基本情况 AS a left join [05个人明细] as c on a.公民身份证=c.公民身份证 where c.时间=(select top 1 b.时间 from [05个人明细] as b where b.公民身份证=a.公民身份证 and b.缴费类型 = '正常应缴' order by b.时间 desc)
SELECT a.*
FROM 05个人明细 AS a
WHERE (((Exists (select b.公民身份证 from [05个人明细] as b where( b.公民身份证=a.公民身份证 and a.缴费类型 = '正常应缴' and b.缴费类型 = '正常应缴' and b.时间>a.时间)))=False) and a.缴费类型 = '正常应缴');
或是
SELECT a.*
FROM 05个人明细 AS a
WHERE (((Not Exists (select b.公民身份证 from [05个人明细] as b where( b.公民身份证=a.公民身份证 and a.缴费类型 = '正常应缴' and b.缴费类型 = '正常应缴' and b.时间>a.时间)))) and a.缴费类型 = '正常应缴');