select a.id,b.name,b.desc,c.cdate from  
tableA as a inner join tableB as b on  a.id=b.id
inner join
(
select top 1 [id],cdate from tableC where id= a.id order by date desc
) as c  on a.id=c.id这语句是错的,我的目地是,返回tableC 表中的日期最近的一个谢谢!

解决方案 »

  1.   

    select a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select top 1 [id],cdate from tableC order by date desc
    ) as c  on a.id=c.id 
      

  2.   

    select a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select top 1 [id],cdate from tableC order by date desc
    ) as c  on a.id=c.id 
      

  3.   

    --2005 CROSS APPLYselect a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    CROSS APPLY
    (
    select top 1 [id],cdate from tableC where id= a.id order by date desc
    ) ;
    --2000select a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select [id],MAX(cdate) AS cdate from tableC GROUP BY id
    ) as c  on a.id=c.id 
      

  4.   

    select a.id,b.name,b.desc,c.cdate 
    from  tableA as a 
    inner join tableB as b 
    on  a.id=b.id 
    inner join 

    select top 1 [id],cdate from tableC where id= a.id order by cdate desc 
    ) c  on a.id=c.id ??
      

  5.   


    select a.id,b.name,b.desc,c.cdate from  
    tableA as a inner join tableB as b on  a.id=b.id 
    inner join 

    select [id],max(cdate) from tableC group by [id] 
    ) as c  on a.id=c.id try..
      

  6.   

    select 
      a.[id],b.[name],b.[desc],c.[cdate] 
    from  
      tableA as a 
    inner join 
      tableB as b 
    on  
      a.id=b.id 
    inner join 
       tableC  as c
    on
      a.id=c.id and not exists(select 1 from tableC where id= a.id and date>c.[date]) 
      

  7.   

    modify
    select a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select [id],MAX(cdate) AS cdate from tableC GROUP BY id
    ) as c  on a.id=c.id 
      

  8.   

    select a.id,b.name,b.desc,c.cdate from  
    tableA as a 
    inner join tableB as b on  a.id=b.id 
    inner join 

    select  [id],max(cdate) from tableC ordy by id

    as c  on a.id=c.id 
      

  9.   

    试了下面这个方法,可行,但也存在一个问题,就是tableC的group by中不能出现其它的列,比如:select [id], subname, MAX(cdate) AS cdate from tableC GROUP BY id,这subname是不同的,所以这方法还是不行。--2000select a.id,b.name,b.desc,c.cdate from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select [id],MAX(cdate) AS cdate from tableC GROUP BY id
    ) as c  on a.id=c.id 大家还有没更好的办法,谢谢!!!!!!
      

  10.   

    select a.id,b.name,b.desc,c.cdate,d.other_col1,d.other_col2 from 
    tableA as a inner join tableB as b on  a.id=b.id
    inner join
    (
    select [id],MAX(cdate) AS cdate from tableC GROUP BY id
    ) as c  on a.id=c.id 
    inner join tbC AS D
     ON c.id=D.id and c.cdate = D.cdate
      

  11.   

    6楼的"and date>c.[date]) "这里报错,以为是会列名写错了,我改成c.[cdate],但还是报错!我这里用的是sql server2000,里面好像没这写法,不过2005我没用过,不清楚了
      

  12.   


    select 
      a.[id],b.[name],b.[desc],c.[cdate] 
    from  
      tableA as a 
    inner join 
      tableB as b 
    on  
      a.id=b.id 
    inner join 
       tableC  as c
    on
      a.id=c.id and not exists(select 1 from tableC where id= c.id and cdate>c.[cdate]) 
    这样呢?
      

  13.   

    Select a.id,
    b.name,
    b.[desc],
    c.cdate 
    From tableA As a 
    Inner Join tableB As b On a.id=b.id
    Inner Join tableC As c On c.id=a.id
    And c.cdate=(Select Max(cdate) From tableC Where id=c.id)