在一个sql查询中  我想取一个表中的前面A条数据,对这几条数据求和,但A是不固定的,A值在同一个查询中得出,这样不知怎么做,不用游标,能否做到如下这个sql要表达的意思所示:
select B,C,(B-C) as A,(这里select一个子查询对一个表中的前面A条数据求和) as D from table

解决方案 »

  1.   

    SQL SERVER 2005中如下:  Create table  table1(
         ID int,
         B int,
         C int
      )
      Create table table2(
         ID int,
         D int 
      )select ID,B,C,D from table1
    以上的  D 等于 table2中a条数据的和  a=B-C
      

  2.   

    select ID,B,C,D from table1
    以上的 D 等于 table2中前a条数据中D字段的和 而a=B-C
      

  3.   


    use tempdb;create table dbo.test_tb1
    (
      id int identity(1,1),
      bvalue int not null,
      cvalue int not null
     )
     
    insert into dbo.test_tb1
    select 13,7  union all
    select 14,9  union all
    select 14,10 union all
    select 15,8  union all
    select 16,6  union all
    select 16,14 union all
    select 17,13 union all
    select 18,17 union all
    select 17,10 ;create table dbo.test_tb2
    (
      id int identity(1,1),
      dvalue int not null
    )insert into dbo.test_tb2
    select number
    from master..spt_values
    where type='p'
     and number between 35 and 75;select id,bvalue,cvalue,
           (select SUM(dvalue)
            from dbo.test_tb2
            where id<=bvalue-cvalue)as dvalue --注:dbo.test_tb2中id是连续的 且前n条就相当于id<=n 否则就要生成行号 然后判断
    from dbo.test_tb1/**
    id          bvalue      cvalue      dvalue
    ----------- ----------- ----------- -----------
    1           13          7           225
    2           14          9           185
    3           14          10          146
    4           15          8           266
    5           16          6           395
    6           16          14          71
    7           17          13          146
    8           18          17          35
    9           17          10          266(9 行受影响)
    **/
      

  4.   

    路过帮你顶下,顺便问下4楼的
    "select number
    from master..spt_values
    where type='p'"
    是啥意思啊?怎么像是从系统表里去数据样呢?
      

  5.   

    /*
    在TOP后面使用变量
    (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)  2008-01-02  广东深圳)
    */--SQL SERVER 2005 的写法
    use adventureworks
    goDECLARE @Percentage int
    SET @Percentage = 1
    SELECT TOP (@Percentage) PERCENT
    Name
    FROM Production.Product
    ORDER BY Name/*
    Name
    ----------------------
    Adjustable Race
    All-Purpose Bike Stand
    AWC Logo Cap
    BB Ball Bearing
    Bearing Ball
    Bike Wash - Dissolver(6 行受影响)
    */-----------------------------------
    --SQL SERVER 2000 的写法
    create table a([id] [int])
    insert into a(id) values(1)
    insert into a(id) values(2)
    insert into a(id) values(3)
    insert into a(id) values(4)
    insert into a(id) values(5)declare @num as int
    declare @sql as varchar(2000)
    set @num = 2
    set @sql = 'select top ' + cast(@num as char) + ' * from a'
    exec(@sql)drop table a
    /*
    id          
    ----------- 
    1
    2
    */
      

  6.   

    谢谢两位的回复,
    爱新觉罗.毓华
    TOP后面可以用变量吗?这个我没有试过,如果能用变量但这个变量能从同一个select中来吗?如果这样可以的话,性能会更高,能用变量也好像不行呀,这个值用于每一行的都不一样的denghui_li table2是没有ID这一个IDENTITY的,有这样一个排序的就好处理了,即使有IDENTITY这样一个列,实际到表中也不一定是连续的。还好是在sql server 2005下面,我在查询table2用 Row_number() over()这个新功能,增加一个像IDENTITY这样的列就搞定了.
      

  7.   

    这个是我的方法,如果那位有更好的方法更告之一下.selec B,C,(select sum(D) from (select row_number() over(order by D DESC) as rowindex,D from table2) as rep_table2) where rowindex<B-C) as D_sum from table1
      

  8.   

    非常正确 用Row_number() over()生成一个行号 放在cte中 或者写成子查询