Select t1.id,t1.name,sum(t2.code) as Code
from table1 as t1 Left join table2 as t2 
   on t1.id=t2.id
group by t1.id,t1.name
order by t1.id

解决方案 »

  1.   

    select a.*,b.code from table1 left join table2 on a.id=b.id where b.datetime=
    (select max(datetime) from table2 where id=a.b.id)
      

  2.   

    看错了
    Select t1.id,t1.name,sum(t2.code) as Code
    from table1 as t1 Left join 
      (Select id,max(datetime) as datetime from table2
       group by id) as t2 on t1.id=t2.id
      inner join table2 on table2.id=t2.id and table2.datetime=t2.datetime
    group by t1.id,t1.name
    order by t1.id
      

  3.   

    declare @a table (
    id     varchar(20),
    name varchar(20)
    )insert @a
    select
                      '001',     'aa'
    union all select
                      '002',     'bb'
    union all select
                      '003',     'cc'declare @b table (
    id     varchar(20),
    code   varchar(20),  
    [datetime] datetime
    )
    insert @b
    select
                       '001',     '12',        '2004-4-4'
    union all select
                       '001' ,    '56' ,       '2004-2-3'
    union all select
                       '001'  ,   '84'  ,      '2004-5-6'
    union all select
                       '002'   ,  '12'   ,     '2004-1-2'
    union all select
                       '002'    , '89'    ,    '2004-7-8'select a.*,b.code from @a a left join @b b 
    on a.id=b.id and b.[datetime]=
    (select max([datetime]) from @b where id=b.id)
      

  4.   

    --结果
    /*
    id                   name                 code                 
    -------------------- -------------------- -------------------- 
    001                  aa                   84
    002                  bb                   89
    003                  cc                   NULL(所影响的行数为 3 行)
    */
      

  5.   

    查询结果不正确呀。
      只是将table1中在table2中有对应值的检索出来了,但是,table1中的记录没有全部显示。
      table1中的记录要全部显示啊。
      请大家赶快帮帮我吧
      谢谢
      

  6.   

    防止万一([datetime]重复):
    declare @a table (
    id     varchar(20),
    name varchar(20)
    )insert @a
    select
                      '001',     'aa'
    union all select
                      '002',     'bb'
    union all select
                      '003',     'cc'declare @b table (
    id     varchar(20),
    code   varchar(20),  
    [datetime] datetime
    )
    insert @b
    select
                       '001',     '12',        '2004-4-4'
    union all select
                       '001' ,    '56' ,       '2004-2-3'
    union all select
                       '001'  ,   '84'  ,      '2004-5-6'
    union all select
                       '002'   ,  '12'   ,     '2004-1-2'
    union all select
                       '002'    , '89'    ,    '2004-7-8'select a.*,isnull((select top 1 code from @b where id=a.id order by [datetime] desc),'-') as code from @a a /*
    结果
    id                   name                 code                 
    -------------------- -------------------- -------------------- 
    001                  aa                   84
    002                  bb                   89
    003                  cc                   -(所影响的行数为 3 行)
    */
      

  7.   

    select a.*,b.code from table1 a full join table2 b on a.id=b.id where b.datetime=
    (select max(datetime) from table2 where id=a.id)
      

  8.   

    '001',     '12',        '2004-4-4'
    union all select
                       '001' ,    '56' ,       '2004-2-3'
    union all select
                       '001'  ,   '84'  ,      '2004-5-6'
    union all select
                       '002'   ,  '12'   ,     '2004-1-2'
    union all select
                       '002'    , '89'    ,    '2004-7-8'上面的是什么意思?我的记录不止这几个
      

  9.   

    问题还没有解决,都是table1的记录不能全部显示
      

  10.   

    select a.*,b.*
    from table1 a
    left join(
    select id,[datetime]=max([datetime])
    from table2
    group by id
    )bb on a.id=bb.id
    left join table2 b on b.id=bb.id and b.[datetime]=bb.[datetime]
      

  11.   

    --测试--测试数据
    declare @a table (id varchar(20),name varchar(20))
    insert @a select '001','aa'
    union all select '002','bb'
    union all select '003','cc'declare @b table (id varchar(20),code varchar(20),[datetime] datetime)
    insert @b select '001','12','2004-4-4'
    union all select '001','56','2004-2-3'
    union all select '001','84','2004-5-6'
    union all select '002','12','2004-1-2'
    union all select '002','89','2004-7-8'--查询
    select a.*,b.*
    from @a a
    left join(
    select id,[datetime]=max([datetime])
    from @b
    group by id
    )bb on a.id=bb.id
    left join @b b on b.id=bb.id and b.[datetime]=bb.[datetime]/*--测试结果id     name   id     code   datetime     
    ------ ------ ------ ------ ----------------------- 
    001    aa     001    84     2004-05-06 00:00:00.000
    002    bb     002    89     2004-07-08 00:00:00.000
    003    cc     NULL   NULL   NULL(所影响的行数为 3 行)
    --*/
      

  12.   

    select a.*,b.code from 
    @a a left join @b b 
    on a.id=b.id and b.datetime=
    (select max(datetime) from @b where id=b.id)   --接上面的,也可以得到结果
      

  13.   

    select a.*,isnull((select top 1 code from Table2 where id=a.id order by [datetime] desc),'-') as code from Table1 a