请各位高手帮帮忙.用一条SQL语句写.
一个表中有n条记录,前4条就正常显示,后面的记录相加后显示
原表
CD    NM      Money
001   aa      100
002   bb      90
003   cc      80
004   dd      120
005   ee      20
006   ff      80查询后显示的结果
CD    NM        Money
001   aa        100
002   bb        90
003   cc        80
004   dd        120
005   そのほか  100

解决方案 »

  1.   

    declare @a table(cd int,NM varchar(10),Money int)
    insert into @a select 1,'aa',100
    union all select 2,'bb',90
    union all select 3,'cc',80
    union all select 4,'dd',120
    union all select 5,'ee',20
    union all select 6,'ff',80
    select * from @a
    select top 4 * from @a union all
    select 005,'そのほか',sum(money) money from @a  where cd > 4
      

  2.   

    都是固定的话可以
    select top 4 * from num union
    select '005','そのほか',sum(Money1) from num where CD not in('001','002','003','004')
      

  3.   

    select top 4 * from num union
    select '005','そのほか',sum(Money1) from num where CD not in(select top 4 CD from num)