有2个表啊: 表名:yh 用户编码  用户名称  
001          a 
002          b 
003          c 表名:ys 
用户编码  本期指数  抄表时间 
  001          11      2009-01-13 
  001          2      2009-02-26 
  002          3      2009-01-13 
  002          1      2009-02-26 
两个表的通过用户编码 来关联: 
希望得到下面的结构   用户编码    用户名称  本期指数 
  001          a        2 
  002          b        1 
  003          c        0 

解决方案 »

  1.   

    SELECT  A.用户编码,用户名称,  本期指数 =COUNT(*)
    FROM YH A,YS B
    WHERE A.用户编码=B.用户编码
    GROUP BY A.用户编码,用户名称
      

  2.   

    select 
      yh.*,
      isnull(ys.本期指数,0) as 本期指数
    from
      yh
    left join
      ys
    on yh.用户编码 =ys.用户编码 
    and not exists(select 1 from ys t where t.用户编码=ys.用户编码 and t.抄表时间>ys.抄表时间)
      

  3.   

    select c.*,t.抄表时间
    from yb c,(
    select *
    from ys a
    where not exists (select 1 
    from ys b
    where a.用户编码=b.用户编码 and a.超标时间<b.抄表时间)
    )t
    where c.用户编码=b.用户编码 
      

  4.   

    select c.*,isnull(t.抄表时间,0)
    from yb c left join (
    select *
    from ys a
    where not exists (select 1 
    from ys b
    where a.用户编码=b.用户编码 and a.超标时间<b.抄表时间)
    )t on c.用户编码=b.用户编码 
      

  5.   

    SELECT  a.用户编码,a.用户名称,  本期指数 =COUNT(*)
    FROM yh a inner join
    ys b on a.用户编码=b.用户编码
    GROUP BY a.用户编码,a.用户名称
      

  6.   

    declare @yh table(用户编码 char(3),用户名称 nvarchar(20))
    insert @yh
    select '001', 'a' union all
    select '002', 'b' union all
    select '003', 'c'declare @ys table(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
    insert @ys
    select '001',11,'2009-01-13' union all
    select '001',2, '2009-02-26' union all
    select '002',3, '2009-01-13' union all
    select '002',1, '2009-02-26'select  
    a.用户编码,用户名称,本期指数=isnull(本期指数,0)
    from 
    @yh a left join @ys b
     on 
    a.用户编码=b.用户编码 and
    b.抄表时间='2009-02-26'/*
    用户编码 用户名称                 本期指数
    ---- -------------------- -----------
    001  a                    2
    002  b                    1
    003  c                    0(3 行受影响)*/
      

  7.   

    SELECT  a.用户编码,a.用户名称,  b.本期指数 =COUNT(*)
    FROM yh a inner join
    ys b on a.用户编码=b.用户编码
    GROUP BY a.用户编码,a.用户名称
      

  8.   

    --try
    ;With china as
    (
      select * from ys  A where 抄表时间= (select max(抄表时间) from ys where a.用户编号=用户编号) 
    )
    select a.* ,isnull(b.本期指数,0) from yh a left join china b on a.用户编号=b.用户编号  
      

  9.   


    create table #yh(用户编码 char(3),用户名称 nvarchar(20))
    insert #yh
    select '001', 'a' union all
    select '002', 'b' union all
    select '003', 'c'create table #ys(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
    insert #ys
    select '001',11,'2009-01-13' union all
    select '001',2, '2009-02-26' union all
    select '002',3, '2009-01-13' union all
    select '002',1, '2009-02-26'select #yh.*,
           isnull(#ys.本期指数,0) as 本期指数
     from #yh left join #ys on #yh.用户编码 =#ys.用户编码 
                          and not exists(select 1 
                                           from #ys t
                                          where t.用户编码=#ys.用户编码 and t.抄表时间>#ys.抄表时间)
    用户编码 用户名称                 本期指数        
    ---- -------------------- ----------- 
    001  a                    2
    002  b                    1
    003  c                    0(所影响的行数为 3 行)
      

  10.   

      create table yh(bm varchar(10),name varchar(3))
      insert yh
      select '001'         , 'a' union all
      select '002'        ,  'b' union all
      select '003'         , 'c' 
      create table ys (bm varchar(10),num int,times datetime)
      insert ys
      select   '001'      ,    11   ,   '2009-01-13' union all
        select '001'     ,     2   ,   '2009-02-26' union all
       select '002'       ,   3    ,  '2009-01-13' union all
      select  '002'       ,   1    ,  '2009-02-26' 
      
    select a.bm,a.name,isnull(b.num,0) from yh a  left join(
    select bm,sum(num) as num from ys where times='2009-02-26 'group by bm)b on a.bm=b.bm
      

  11.   

    create table #yh(用户编码 char(3),用户名称 nvarchar(20))
    insert #yh
    select '001', 'a' union all
    select '002', 'b' union all
    select '003', 'c'create table #ys(用户编码 char(3),本期指数 int,抄表时间 nvarchar(12))
    insert #ys
    select '001',11,'2009-01-13' union all
    select '001',2, '2009-02-26' union all
    select '002',3, '2009-01-13' union all
    select '002',1, '2009-02-26'select a.用户编码,a.用户名称,isnull(本期指数,0)as  本期指数 from #yh a full outer join 
    (select 用户编码 ,本期指数 from #ys t where  not exists 
    (select 1 from #ys where 用户编码=t.用户编码 and 抄表时间>t.抄表时间)) b 
    on a.用户编码=b.用户编码001 a 2
    002 b 1
    003 c 0
      

  12.   


    if(object_id('yh') > 0)
      drop table yh
    go
    create table yh(
        usercode nchar(3),
        username nchar(1)
    )
    insert into yh select '001','a' union all select '002','b' union all select '003','c'
    if(object_id('ys') > 0)
      drop table ys
    go
    create table ys(
        usercode nchar(3),
        lennum int,
        date datetime
    )
    insert into ys select '001','11','2009-01-31' 
    union all select '001',2,'2009-02-26'
    union all select '002',3,'2009-01-13'
    union all select '002',1,'2009-02-26'
    select a.usercode,a.username,isnull(b.lennum,0) as lennum from yh a left join (select * from ys a where not exists(select 1 from ys  where usercode = a.usercode and date > a.date)) b 
    on a.usercode = b.usercode