数据库是:ACCESS日期(1)表:           日期(2)表:
姓名  日期              姓名  日期 
aaa 2000-1-1           aaa   2004-1-1  
aaa  2000-2-12          bbb   2004-5-14
bbb  2000-1-1           ccc   2004-8-7 
ccc  2000-4-24          aaa   2004-9-8
bbb  2000-5-10          ccc   2004-8-1
ccc  2000-1-1           bbb   2004-3-1 
..                      ..  
..                      ..得到如下结果:总表
姓名 日期(1)  日期(2)
aaa   2000-2-12   2004-9-8
bbb   2000-5-10   2004-5-14
ccc   2000-4-24   2004-8-7
..
..总表中的每个姓名的日期(1)、日期(2)的字段值,分别取日期(1)表、日期(2)表中对应姓名的
日期的最大值。小弟已经使用的方法很笨的方法,更新时速度不佳。请各位帮帮忙!谢谢啦!!!!

解决方案 »

  1.   

    select A.Name, Max(A.Date), Max(B.Date) from Table1 as A, Table2 as B
    where (A.Name=B.Name) group by (A.Name)这样不行吗?
      

  2.   

    select A.姓名,A.日期 as 日期(1),B.日期 as 日期(2)
    from (select 姓名, max(日期) from 日期(1)表  group by 姓名 ) A
         left join 
         (select 姓名, max(日期) from 日期(2)表  group by 姓名 ) B
         on A.姓名=B.姓名
      

  3.   

    update 总表, (select A.姓名,A.日期 as 日期(1),B.日期 as 日期(2)
    from (select 姓名, max(日期) from 日期(1)表  group by 姓名 ) A
         left join 
         (select 姓名, max(日期) from 日期(2)表  group by 姓名 ) B
         on A.姓名=B.姓名) Cset 总表.日期(1)=C.日期(1),总表.日期(2)=C.日期(2)
    where 总表.姓名=C.姓名如果提示“必须是可更新的数据集”
    只能借助中间表 temp
    //先插入temp
    select A.姓名,A.日期 as 日期(1),B.日期 as 日期(2) into temp
    from (select 姓名, max(日期) from 日期(1)表  group by 姓名 ) A
         left join 
         (select 姓名, max(日期) from 日期(2)表  group by 姓名 ) B
         on A.姓名=B.姓名
    //更新总表
    update 总表, temp
    set 总表.日期(1)=temp.日期(1),总表.日期(2)=temp.日期(2)
    where 总表.姓名=temp.姓名
    //删除临时表temp
    drop table temp
      

  4.   

    select a.xm,a.rq as totrq1,b.rq as totrq2 into temp
    from (select xm, max(rq) from tbxdq  group by xm ) A
         left join 
         (select xm, max(rq) from tebdq  group by xm ) B
         on a.xm=b.xm
    运行时,提示:参数不足,期待是2
      

  5.   

    日期(1)表:(tbxdq)           日期(2)表:(tebdq)
    姓名(xm)  日期(bxdq)           姓名(xm)  日期(ebdq) 
    aaa      2000-1-1             aaa       2004-1-1  
    aaa       2000-2-12            bbb       2004-5-14
    bbb       2000-1-1             ccc       2004-8-7 
    ccc       2000-4-24            aaa       2004-9-8
    bbb       2000-5-10            ccc       2004-8-1
    ccc       2000-1-1             bbb       2004-3-1 
    ..                             ..  
    ..                             .. 
    得到如下结果:总表(gabase)
    姓名(xm) 日期(1)(bxdq)  日期(2)(ebdq)
    aaa       2000-2-12         2004-9-8
    bbb       2000-5-10         2004-5-14
    ccc       2000-4-24         2004-8-7
    ..
    ..运行:
    select a.xm,a.bxdq as totrq1,b.ebdq as totrq2 into temp
    from (select xm, max(bxdq) from tbxdq  group by xm ) A
         left join 
         (select xm, max(ebdq) from tebdq  group by xm ) B
         on a.xm=b.xm
    提示:参数不足,期待是2。
      

  6.   

    select a.xm ,a.a_rq as totrq1,b.b_rq as totrq2 into temp
    from (select xm,max(rq) as [a_rq] from tbxdq  group by xm ) A 
         left join 
         (select xm, max(rq) as [b_rq] from tebdq  group by xm ) B on a.xm=b.xm
      

  7.   

    UPDATE 总表 SET 日期1 = A.日期1, 日期2 = B.日期2 
    FROM 总表
    JOIN (SELECT MAX(日期) AS 日期1 ,姓名 FROM 日期1表 GROUP BY 姓名) A 
    ON 总表.姓名 = A.姓名
    JOIN (SELECT MAX(日期) AS 日期2 ,姓名 FROM 日期2表 GROUP BY 姓名) B 
    ON 总表.姓名 = B.姓名INSERT 总表 (姓名,日期1,日期2)
    SELECT C.姓名, MAX(C.日期1), MAX(C.日期2) FROM 
    (SELECT MAX(日期) AS 日期1 ,NULL AS 日期2, 姓名 
    FROM 日期1表 WHERE 姓名 NOT IN (SELECT DISTINCT 姓名 FROM 总表) GROUP BY 姓名
    UNION
     SELECT NULL AS 日期1, MAX(日期) AS 日期2 ,姓名 
    FROM 日期2表 WHERE 姓名 NOT IN (SELECT DISTINCT 姓名 FROM 总表) GROUP BY 姓名) C
    GROUP BY C.姓名
      

  8.   

    to qizhanfeng(glacier):select a.xm ,a.a_rq as totrq1,b.b_rq as totrq2 into temp
    from (select xm,max(rq) as [a_rq] from tbxdq  group by xm ) A 
         left join 
         (select xm, max(rq) as [b_rq] from tebdq  group by xm ) B on a.xm=b.xm
    能不能得到两个表的并集。现在得到的总表数据是:如果表2中有ggg,表1中没有ggg,总表中就没有ggg。而我想要的是总表中也有ggg这条记录。如果表1中没有ggg,那么在总表中的ggg的日期(1)字段就为空。相反表1中有,表2中没有的姓名,也要是这样。(我的表达能力太差)得到如下结果:总表(gabase)
    姓名(xm) 日期(1)(bxdq)  日期(2)(ebdq)
    aaa       2000-2-12         2004-9-8
    bbb       2000-5-10         2004-5-14
    ccc       2000-4-24         2004-8-7
    eee       2000-9-6
    ggg                         2008-7-4 
    ..qizhanfeng(glacier)大哥,帮帮忙,分不够可以加!!!!!谢谢!!!!
      

  9.   

    select a.xm ,a.a_rq as totrq1,b.b_rq as totrq2 
    from (select xm,max(rq) as [a_rq] from tbxdq  group by xm ) A 
         left join 
         (select xm, max(rq) as [b_rq] from tebdq  group by xm ) B on a.xm=b.xm
    union all
    select xm,null,rq 
    from tebdq 
    where not exists( select * from tbxdq where tebdq.xm=tbxdq.xm)  
      

  10.   

    问题差不多解决了,多谢qizhanfeng(glacier) ,但小弟还有一点小问题,就是如何将上述语句生成 的数据保存到表。
      

  11.   

    select * into temp from (select a.xm ,a.a_rq as totrq1,b.b_rq as totrq2 
    from (select xm,max(rq) as [a_rq] from tbxdq  group by xm ) A 
         left join 
         (select xm, max(rq) as [b_rq] from tebdq  group by xm ) B on a.xm=b.xm
    union all
    select xm,null,rq 
    from tebdq 
    where not exists( select * from tbxdq where tebdq.xm=tbxdq.xm))
      

  12.   

    select A.Name, A.Date, B.Date from Table1 as A, Table2 as B
    where (A.Name=B.Name) order by a.name