当前位置: 代码迷 >> Sql Server >> 怎么获取另外一个表中距离A表时间最近的那条记录
  详细解决方案

怎么获取另外一个表中距离A表时间最近的那条记录

热度:86   发布时间:2016-04-24 09:11:25.0
如何获取另外一个表中距离A表时间最近的那条记录

select a.*,
RID = (select top 1 ID from b where a.mdate> b.ddate order by b.ddate desc),
dt = (select top 1 ddate from b where a.mdate> b.ddate order by b.ddate desc)
 from a



这个语句是我自己写的,但是执行效率很一般。怎么提高效率
------解决思路----------------------
    select a.*,
           b.id RID,
           b.ddate
      from (SELECT a1.mdate,
                   MAX(b1.ddate) ddate
              FROM a a1
              JOIN b b1
                ON a1.mdate > b.ddate
           ) c
      JOIN a
        ON c.mdate = a.mdate
 LEFT JOIN b
        ON c.ddate = b.ddate

效率就降了。
Access的自己翻译。
  相关解决方案