错了,结果应为
xm  zhi  lj
-------------
a    5    9
b    0    2
c    0    4

解决方案 »

  1.   

    select xm,sum(zhi) from table1 where id='200303' group by xm
      

  2.   

    select xm,sum(zhi) from table1 where id like '200303%' group by xm
      

  3.   

    select xm,sum(zhi) from table1 where id='200303' group by xm
    支持飞雨的
      

  4.   

    select xm,sum(zhi) from table1 where id like '200303%' group by xm
    支持楼上
      

  5.   

    select a.xm,a.zhi,b.lj from (select xm,max(zhi) as zhi from table where id ='200303' group by xm) a left join (select xm,sum(zhi) as lj from table where id <='200303' group by xm) b have on a.xm =b.xm
      

  6.   

    select a.xm,a.zhi,b.lj from (select xm,max(zhi) as zhi from table where id ='200303' group by xm) a left join (select xm,sum(zhi) as lj from table where id <='200303' group by xm) b on a.xm =b.xm错了,应该没有have
      

  7.   

    zw_yu(鱼猫) ,你的方法我试了一下,只能生成如下:
    xm    zhi    lj
    a      5      9可是b,c的值呢?应形成如下所示:
    xm  zhi  lj
    -------------
    a    5    9
    b    0    2
    c    0    4
      

  8.   

    select a.xm,isnull(b.zhi,0) as zhi,c.lj
    from (
    selec distinct xm from table) a
    left join
    (select xm,zhi from table where id='20030') b on a.xm=b.xm
    left join (select xm,sum(zhi) as lj where id<'20030' group by xm) c
    on a.xm=c.xm 
      

  9.   

    你还是写个存储过程吧!这是代码:
    create proc User_Print
    as 
    declare @xm varchar(2),@zhi int,@lj int
    declare cur_tbl cursor for 
    select a.xm,a.zhi,b.lj from (select xm,max(zhi) as zhi from tbl where id ='200303' group by xm) a left join (select xm,sum(zhi) as lj from tbl where id <='200303' group by xm) b on a.xm =b.xm
    open cur_tbl
    fetch next from cur_tbl into @xm,@zhi,@lj
    while (@@fetch_status<>-1)
    begin
    print @xm
    fetch next from cur_tbl into @xm,@zhi,@lj
    end
    close cur_tbl
    deallocate cur_tbl
    select a.xm,a.zhi,b.lj from (select xm,max(zhi) as zhi from tbl where id ='200303' group by xm) a left join (select xm,sum(zhi) as lj from tbl where id <='200303' group by xm) b on a.xm =b.xm
    declare cur_table cursor for
    select a.xm,a.zhi,b.lj from (select xm,max(zhi)as zhi from tbl where xm <> @xm group by xm) a left join (select xm,sum(zhi) as lj from tbl where xm <> @xm  group by xm ) b on a.xm=b.xm
    open cur_table
    fetch next from cur_table into @xm,@zhi,@lj
    if (@zhi=@lj)
    begin
    set @zhi=0
    select @xm,@zhi,@lj
    end
    while(@@fetch_status <>-1)
    begin
    fetch next from cur_table into @xm,@zhi,@lj
    if (@zhi=@lj)
    begin
    set @zhi=0
    select @xm,@zhi,@lj
    end
    end
    close cur_table
    deallocate cur_table
      

  10.   

    enhydraboy(努力学习C#) 的语句有错,应该是select a.xm,isnull(b.zhi,0) as zhi,c.lj from (select xm from tbl) a left join(select xm,zhi from tbl where id='200303') b on a.xm=b.xm left join (select xm,sum(zhi) as lj from tbl where id <>'200303' group by xm) c on a.xm=c.xm 
      

  11.   

    综合以上大家的意见,我总结了一下,像这样就可以得到结果:
    select  a.xm,isnull(b.zhi,0)  as  zhi,c.lj  
    from  
    (select  distinct xm  from  ttt)  a  
    left  join
    (select  xm,zhi  from  ttt  where  id='200303')  b  on  a.xm=b.xm  
    left  join  
    (select  xm,sum(zhi)  as  lj  from  ttt   group  by  xm)  c  on  a.xm=c.xm    谢谢楼上的朋友们!