有张表a:
流水号   科目    成绩   
001      语文     90
002      数学     89
003      英语     99
004      地理     96
......怎么查询到下面的效果:流水号   科目    成绩    累计数
001      语文     90        90
002      数学     89        179
003      英语     99        276
004      地理     96        374
......

解决方案 »

  1.   

    select *,累计数=(select sum(累计数) from ta where 流水号 <= a.流水号)
    from ta a
      

  2.   

    create table a(lsh varchar(10), km varchar(10), cj int);
    insert into a(lsh,km,cj)
    select
    '001','语文',90 union all select
    '002','数学',89 union all select
    '003','英语',99 union all select
    '004','地理',96;select a.lsh, a.km, a.cj, sum(b.cj)
    from a a left join a b
    on a.lsh>=b.lsh
    group by a.lsh, a.km, a.cj;
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-28 16:55:39
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([流水号] varchar(3),[科目] varchar(4),[成绩] int)
    insert [tb]
    select '001','语文',90 union all
    select '002','数学',89 union all
    select '003','英语',99 union all
    select '004','地理',96
    --------------开始查询--------------------------
    select
     *,累计数=(select sum(成绩) from tb where 流水号 <= t.流水号) 
    from
     tb t 
    ----------------结果----------------------------
    /* 流水号  科目   成绩          累计数
    ---- ---- ----------- -----------
    001  语文   90          90
    002  数学   89          179
    003  英语   99          278
    004  地理   96          374(4 行受影响)
    */
      

  4.   

    /*====================================================*/
    -- Author: Ken Wong
    -- Create date: 2009-12-28 16:57:47
    -- Description:
    /*====================================================*/
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([流水号] varchar(3),[科目] varchar(4),[成绩] int)
    insert [tb]
    select '001','语文',90 union all
    select '002','数学',89 union all
    select '003','英语',99 union all
    select '004','地理',96select *,
    (select sum(成绩) from [tb] where 流水号<=t.流水号) as 累计数
    from [tb] t
    ------------------------
    001 语文 90 90
    002 数学 89 179
    003 英语 99 278
    004 地理 96 374
      

  5.   

    1> select * from tb
    2> go
    流水号|科目  |成绩
    ---|----|-----------
    001|语文  |         90
    002|数学  |         89
    003|英语  |         99
    004|地理  |         96(4 rows affected)
    1> select 流水号,科目,成绩,
    2>      (select sum(成绩) from tb where 流水号<=t.流水号) as 累计数
    3> from tb t
    4> order by 1
    5> go
    流水号|科目  |成绩         |累计数
    ---|----|-----------|-----------
    001|语文  |         90|         90
    002|数学  |         89|        179
    003|英语  |         99|        278
    004|地理  |         96|        374(4 rows affected)
    1>
      

  6.   

    1> select t1.流水号,t1.科目,t1.成绩,sum(t2.成绩) as 累计数
    2> from tb t1,tb t2
    3> where t1.流水号>=t2.流水号
    4> group by t1.流水号,t1.科目,t1.成绩
    5> order by 1
    6> go
    流水号|科目  |成绩         |累计数
    ---|----|-----------|-----------
    001|语文  |         90|         90
    002|数学  |         89|        179
    003|英语  |         99|        278
    004|地理  |         96|        374(4 rows affected)
    1>
      

  7.   

    select *,
    累计数=(select SUM(成绩) from a as b where 流水号<=a.流水号)
     from a 
      

  8.   

    create table [tb]([流水号] varchar(3),[科目] varchar(4),[成绩] int)
    insert [tb]
    select '001','语文',90 union all
    select '002','数学',89 union all
    select '003','英语',99 union all
    select '004','地理',96select t.* , 累计数 = (select sum(成绩) from tb where 流水号 <= t.流水号)
    from tb tdrop table tb/*
    流水号  科目   成绩          累计数         
    ---- ---- ----------- ----------- 
    001  语文   90          90
    002  数学   89          179
    003  英语   99          278
    004  地理   96          374(所影响的行数为 4 行)
    *.
      

  9.   

    select*,累计数=(select sum(成绩) from tb where 流水号 <= t.流水号) 
    from
     tb t