省份  月份 1 2 3 4 5
山东         10 15 20 20 25
广西         15 20 25 20 25
江苏         20 25 30 35 40
广东         25 30 30 35 45
上海         30 40 50 55 45想要得到如下查询结果
累计为:1-5月总和,最末月为5月,上月为4月
=========================
累计 最末月 上月
山东 90 25 20
广西 105 25 20
江苏 150 40 35
广东 165 45 35
上海 220 45 55

解决方案 »

  1.   


    select 省份 as[累计],(月份1+2+3+4+5) as 总和,月份5 as [最末月],月份四 as [上月]
    from tb 
      

  2.   

    [code=SQ]
    select 省份 as[累计],(1+2+3+4+5) as 总和,5 as [最末月],4 as [上月]
    from tb
    [/code]
      

  3.   

    select 省份,(1+2+3+4+5) as [累计],5 as [最末月],4 as [上月] 
    from tb
      

  4.   

    select 省份,(1+2+3+4+5) as [累计],5 as [最末月],4 as [上月]  
    from tb
    ===============
    累计 总和 最末月 上月
    山东 15 5 4
    广西 15 5 4
    江苏 15 5 4
    广东 15 5 4
    上海 15 5 4
    ==========
    要求达到如下结果
    ==========
         累计 最末月 上月 
    山东 90    25    20 
    广西 105   25    20 
    江苏 150   40    35 
    广东 165   45    35 
    上海 220   45    55 
      

  5.   


    你的月份列是  1、2、3、4、5 ???
    那么select 省份,([1]+[2]+[3]+[4]+[5]) as [累计],[5] as [最末月],[4] as [上月]  
    from tb
      

  6.   

    [code=SQ]select 省份 as[累计],(月份1+2+3+4+5) as 总和,月份5 as [最末月],月份四 as [上月]
    from tb [/code]
      

  7.   

    select 省份 as '累计',(月份1+2+3+4+5) as 总和,月份5 as '最末月',月份4 as '上月'
    from tb
      

  8.   

    [code=SQ]
    select 省份 as[累计],(1+2+3+4+5) as 总和, 5 as [最末月],4 as [上月] from tb
    [/code]
      

  9.   


    select 省份 as[累计],(1+2+3+4+5) as 总和, 5 as [最末月],4 as [上月] from tb 
      

  10.   

    [code=SQ]
    use tempdb;
    /*
    create table test
    (
    省份 nvarchar(10) not null,
    [1月] int not null,
    [2月] int not null,
    [3月] int not null,
    [4月] int not null,
    [5月] int not null
    );
    insert into test(省份,[1月],[2月],[3月],[4月],[5月])
    values
    ('山东',10,15,20,20,25),
    ('广西',15,20,25,20,25),
    ('江苏',20,25,30,35,40),
    ('广东',25,30,30,35,45),
    ('上海',30,40,50,55,45);
    */
    select 省份,[1月]+[2月]+[3月]+[4月]+[5月] as [累积],[5月] as [最末月],[4月] as [上月]
    from test;
    [/code]
      

  11.   

    create table #temp
    (
    省份 nvarchar(10),
    月份 int,
    [1] int,
    [2] int,
    [3] int,
    [4] int,
    [5] int
    )
    insert #temp(省份,[1],[2],[3],[4],[5])
    select N'山东',10,15,20,20,25 union all
    select N'广西',15,20,25,20,25 union all
    select N'江苏',20,25,30,35,40 union all
    select N'广东',25,30,30,35,45 union all
    select N'上海',30,40,50,55,45
    select 
    省份,
    累计 = [1]+[2]+[3]+[4]+[5],
    最末月 = [5],
    上月 = [4]
     from #temp