我的问题实际上是要解决以下内容:
    表A            表B                 表C
name    a      name  date  b       name  date  c
mm     A.x     mm    1988  B.x     mm    1985  C.x
gg     A.y     mm    1986  B.y     mm    1987  C.y
               gg    1988  B.z     gg    1989  C.z
               gg    1989  B.u     gg    1986  C.u 
如果指定时间为1989,我通过查询得到以下结果
name  a    b    c
mm   A.x  B.x  C.y 
gg   A.y  B.u  C.z
也就是说:对于每一个name来说,如果有指定时间下的数据,就取指定时间的数据,如果没有,就取离指定时间最近的数据。
请各位赐教!

解决方案 »

  1.   

    select table_B.name,table_A.a,table_C.c from table_A,table_B,table_C where
    table_B.date='1989' and table_1.name=table_b.name and table_C.name=table_b.name
    没试
      

  2.   

    如果是小于的话:
      select 表A.a,表b.b,表c.c from 表A,(select top 1 * from 表b where date<=给定的日期 order by date ) as 表b,(select top 1 * from 表c where date<=给定的日期 order by date ) as 表c where 表A.name=表b.name and 表a.name=表C.name and 表A.name='要查的姓名'
      

  3.   

    没有具体写,大概可以这样写
    select iif(判断条件,真返回等于得数据,假是一个符合条件) from 表 group by nameiif 里怎么写是比较关键得,我晚些时候在看看有没有好方法
      

  4.   

    To:Tanlijun天下
    select top 1 * from 表b where date<=给定的日期 order by date 是什么意思啊 我还没见过 Top 1 * 这样的写法啊
      

  5.   

    select top 1 ... 是取满足条件的第一个记录
      

  6.   

    为什么我用例如下面的语句会出现错误:“General SQL error Ora-00936 缺少表达式”select 表b.b from (select top 1 * from 表b where date<=给定的日期 order by date ) as 表b
      

  7.   

    写错了如果是小于的话。。应该再加个降序排列
      select 表b.b from (select top 1 * from 表b where date<=给定的日期 order by date desc ) as 表b
     它的意思是取日期小于某个张定日期的最近一行。我试了没错啊
      

  8.   

    上面的这条语句好像错误啊,数据库根本不认识Top 1 这样的写法。我的数据库是Oracle
      

  9.   

    select 表A.a,表b.b,表c.c from 表A,(select top 1 * from 表b where date<=给定的日期 order by date ) as 表b,(select top 1 * from 表c where date<=给定的日期 order by date ) as 表c where 表A.name=表b.name and 表a.name=表C.name and 表A.name='要查的姓名'
      

  10.   

    oracle 不支持 select top 1 ...
    不过可以这样写:
    select * from ... where rownum <= 1
    不记得是 rownum 还是 rowid 了,你自己试试另外... from (select ...) as 表b 在 oracle 里也是不对的,表b前面不要写as,写了 oracle 就不认识了
      

  11.   

    楼主,其实两个问题都可以用同样的方法解决
    用我在
    http://expert.csdn.net/Expert/topic/2914/2914810.xml?temp=.1338312
    的回复,可以解决这个问题。要举一反三~~
      

  12.   

    谢谢,sure_ok(8月19日晴)和huawuxin(花无心),我再去尝试尝试