题目如下:
  通过表1的信息 ,用SQL语句实现表2的结果!
  year  salary(表1)
  2000  1000
  2001  2000
  2002  3000
  2003  4000
  2004  5000  year  salary(表2)
  2000  1000
  2001  3000
  2002  6000
  2003  10000
  2004  15000
 

解决方案 »

  1.   


    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([year] int,[salary] int)
    insert [TB]
    select 2000,1000 union all
    select 2001,2000 union all
    select 2002,3000 union all
    select 2003,4000 union all
    select 2004,5000select [year],[salary]=(select sum([salary]) from TB where [year]<=t.[year]) from [TB] t/*
    year        salary
    ----------- -----------
    2000        1000
    2001        3000
    2002        6000
    2003        10000
    2004        15000(5 行受影响)*/drop table TB
      

  2.   

    select [year]
    ,(select sum(salary) from 表1 A where A.year<=year) as 'salary' 
    from  表1
      

  3.   

    select [year]
    ,(select sum(salary) from 表1  where year<=A.year) as 'salary' 
    from  表1 A
      

  4.   

    select a.[year],sum(b.salary) as salary
    from tb a
    join tb b on a.[year]>=b.[year]
    group by a.[year]/**
    year        salary      
    ----------- ----------- 
    2000        1000
    2001        3000
    2002        6000
    2003        10000
    2004        15000(所影响的行数为 5 行)
    **/
      

  5.   


    select a.[year],sum(b.salary) as salary
    from 表1 a
    join 表2 b on a.[year]>=b.[year]
    group by a.[year]
      

  6.   

    select [year],
           [salary]=(select sum([salary]) from TB where [year]<=t.[year]) 
    from [TB] t
      

  7.   

    select 
      [year],
      [salary]=(select sum([salary]) from 表 A where A.[year]<=B.[year]) 
    from 
      表 B
      

  8.   

    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([year] int,[salary] int)
    insert [TB]
    select 2000,1000 union all
    select 2001,2000 union all
    select 2002,3000 union all
    select 2003,4000 union all
    select 2004,5000select [year],
           (select sum([salary]) from [TB] where [year]<=t.[year]) [salary]
    from  [TB] Tyear        salary
    ----------- -----------
    2000        1000
    2001        3000
    2002        6000
    2003        10000
    2004        15000(5 行受影响)
      

  9.   

    if OBJECT_ID('tbb') is not null drop table tbb
    create table tbb(y int,s int)
    insert into tbb
    select 2000,1000 union all
    select 2001,2000 union all
    select 2002,3000 union all
    select 2003,4000 union all
    select 2004,5000select aa.y,sums=(select SUM(bb.s) from tbb as bb where bb.y<=aa.y)
    from tbb as aadrop table tbb
      

  10.   

    select a.yearNO,(select sum(salary) from test where yearNO < a.yearNO) from test a
      

  11.   

    if object_id('tb') is not null  drop table tb
    create table tb(year datetime,salary int)
    insert tb
    select '2000',1000 union all
    select '2001',2000 union all
    select '2002',3000 union all
    select '2003',4000 union all
    select '2004',5000
    select convert(char(4),year,112) as year,(select sum(salary) from tb where year<=a.year) as salary
    from tb a