sourceTable:它是一张有序表,按照季度和单位排序
季度     单位  当期 当期环比 
200801    dw1   2     5
200802    dw1   3     4
200802    dw1   0     1
200802    dw2   2     4
200803    dw1   1     3
200901    dw1   2     4
欲得到下面的结果:
季度     单位  当期    当期环比 当期累计  当期环比累计
200801    dw1   2        5        2          5
200802    dw1   3+0      4+1      2+3+0      5+4+1
200802    dw2   2        4        2          4
200803    dw1   1        3        2+3+0+1    5+4+1+3
200901    dw1   2        4        2          4
 
得到下一张表的方法是这样的:
当期和当期环比分别是本单位本季度的当期和当期环比的累加,而当期累计和当期环比累计分别是本单位并且小于等于本年本季度的当期和当期环比的累加
要优化的部分在于:
因为第一张表是一个按照季度和单位排过序的表,所以当求200802季度的当期和当期累计的时候,查询到200803年的时候就不再继续往下查询做累计了,这样可以节省做累计的时间。因为表中有几十万条记录,所以这样优化一下节省的时间是很可观的,请高手不吝赐教!谢谢~

解决方案 »

  1.   


    create table t(季度  datetime,  单位 varchar(3), 当期 int, 当期环比 int)
    insert  t select 
    '200801',    'dw1'  ,2,    5 union all select
    '200802' ,   'dw1'  ,3 ,   4 union all select
    '200802'  ,  'dw1'  ,0  ,  1 union all select
    '200802'   , 'dw2'  ,2   , 4 union all select
    '200803'    ,'dw1' , 1    ,3 union all select
    '200901'    ,'dw1',  2    ,4 select 季度,单位,当期=sum(当期),当期环比=sum(当期环比),
    当期累计=(select sum(当期) from t where 单位=t1.单位 and 季度<=t1.季度),
    当期环比累计=(select sum(当期环比) from t where 单位=t1.单位 and 季度<=t1.季度)
    from t t1
    group by 季度,单位季度                      单位   当期          当期环比        当期累计        当期环比累计
    ----------------------- ---- ----------- ----------- ----------- -----------
    2020-08-01 00:00:00.000 dw1  2           5           2           5
    2020-08-02 00:00:00.000 dw1  3           5           5           10
    2020-08-03 00:00:00.000 dw1  1           3           6           13
    2020-09-01 00:00:00.000 dw1  2           4           8           17
    2020-08-02 00:00:00.000 dw2  2           4           2           4(5 行受影响)drop table t
      

  2.   


    create table t(季度  datetime,  单位 varchar(3), 当期 int, 当期环比 int)
    insert  t select 
    '200801',    'dw1'  ,2,    5 union all select
    '200802' ,   'dw1'  ,3 ,   4 union all select
    '200802'  ,  'dw1'  ,0  ,  1 union all select
    '200802'   , 'dw2'  ,2   , 4 union all select
    '200803'    ,'dw1' , 1    ,3 union all select
    '200901'    ,'dw1',  2    ,4 select 季度,单位,当期=sum(当期),当期环比=sum(当期环比),
    当期累计=(select sum(当期) from t where 单位=t1.单位 and 季度<=t1.季度 and convert(varchar(4),季度)=convert(varchar(4),t1.季度 )),
    当期环比累计=(select sum(当期环比) from t where 单位=t1.单位 and 季度<=t1.季度 and convert(varchar(4),季度 )=convert(varchar(4),t1.季度 ))
    from t t1
    group by 季度,单位
    order by 季度季度                      单位   当期          当期环比        当期累计        当期环比累计
    ----------------------- ---- ----------- ----------- ----------- -----------
    2020-08-01 00:00:00.000 dw1  2           5           2           5
    2020-08-02 00:00:00.000 dw1  3           5           5           10
    2020-08-02 00:00:00.000 dw2  2           4           2           4
    2020-08-03 00:00:00.000 dw1  1           3           6           13
    2020-09-01 00:00:00.000 dw1  2           4           2           4(5 行受影响)
    drop table t---修改一下
      

  3.   

    学习了,我把季度类型直接改成varchar(6)。不知道楼主要的是不是这个
      

  4.   


    create table t(季度  varchar(6),  单位 varchar(3), 当期 int, 当期环比 int)
    insert  t select 
    '200801',    'dw1'  ,2,    5 union all select
    '200802' ,   'dw1'  ,3 ,   4 union all select
    '200802'  ,  'dw1'  ,0  ,  1 union all select
    '200802'   , 'dw2'  ,2   , 4 union all select
    '200803'    ,'dw1' , 1    ,3 union all select
    '200901'    ,'dw1',  2    ,4 select 季度,单位,当期=sum(当期),当期环比=sum(当期环比),
    当期累计=(select sum(当期) from t where 单位=t1.单位 and 季度<=t1.季度 and left(季度,4)=left(t1.季度,4)),
    当期环比累计=(select sum(当期环比) from t where 单位=t1.单位 and 季度<=t1.季度 and left(季度,4)=left(t1.季度,4))
    from t t1
    group by 季度,单位
    order by 季度季度     单位   当期          当期环比        当期累计        当期环比累计
    ------ ---- ----------- ----------- ----------- -----------
    200801 dw1  2           5           2           5
    200802 dw1  3           5           5           10
    200802 dw2  2           4           2           4
    200803 dw1  1           3           6           13
    200901 dw1  2           4           2           4(5 行受影响)drop table t
      

  5.   

    源表本身就是已经排好序的表了,所以最后也不用order by 了
      

  6.   

    create view VN as select distinct 季度,单位,产品,客户,当期=sum(当期),当期环比=sum(当期环比),当期同比=sum(当期同比),                                 
                                       当期累计=(select sum(当期) from sourceTable  where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100=t.季度/100 and 季度<=t.季度),
                                       当期环比累计=(select sum(当期环比) from sourceTable where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100=t.季度/100 and 季度<=t.季度),
                                       当期同比累计=(select sum(当期同比) from sourceTable where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100<=t.季度/100 and 季度<=t.季度)
    from sourceTable t
    group by 季度,单位,产品,客户
      

  7.   


    create table t(季度  datetime,  单位 varchar(3), 当期 int, 当期环比 int)
    insert  t select 
    '20080101',    'dw1'  ,2,    5 union all select
    '20080201' ,   'dw1'  ,3 ,   4 union all select
    '20080201'  ,  'dw1'  ,0  ,  1 union all select
    '20080201'   , 'dw2'  ,2   , 4 union all select
    '20080301'    ,'dw1' , 1    ,3 union all select
    '20090101'    ,'dw1',  2    ,4 if object_id('tempdb..#') is not null
    drop table #
    select  季度
    ,单位
    ,当期=sum(当期)
    ,当期环比=sum(当期环比)
    into #
    from t
    group by  季度,单位select a.*
    ,当期累计 = sum(b.当期) 
    ,当期环比累计 = sum(b.当期环比)
    from # a  
    left join # b  on a.季度>=b.季度 and a.单位 = b.单位 and convert(varchar(4),b.季度,112)=convert(varchar(4),a.季度,112)group by a.季度,a.单位,a.当期,a.当期环比 drop table t季度                                                     单位   当期          当期环比        当期累计        当期环比累计      
    ------------------------------------------------------ ---- ----------- ----------- ----------- ----------- 
    2008-01-01 00:00:00.000                                dw1  2           5           2           5
    2008-02-01 00:00:00.000                                dw1  3           5           5           10
    2008-02-01 00:00:00.000                                dw2  2           4           2           4
    2008-03-01 00:00:00.000                                dw1  1           3           6           13
    2009-01-01 00:00:00.000                                dw1  2           4           2           4
    借了 小卒哥的数据。不知道合不合格
      

  8.   

    create view VN as select distinct 季度,单位,产品,客户,当期=sum(当期),当期环比=sum(当期环比),当期同比=sum(当期同比),                                
                                      当期累计=(select sum(当期) from sourceTable  where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100=t.季度/100 and 季度 <=t.季度), 
                                      当期环比累计=(select sum(当期环比) from sourceTable where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100=t.季度/100 and 季度 <=t.季度), 
                                      当期同比累计=(select sum(当期同比) from sourceTable where 单位=t.单位 and 产品=t.产品 and 客户=t.客户 and 季度/100 <=t.季度/100 and 季度 <=t.季度) 
    from sourceTable t 
    group by 季度,单位,产品,客户
      

  9.   

    各位,不好意思哈,
    我最终想实现的是在求某一年某一季度的这些值的时候不用去遍历表中该年该季度以后的值。
    在我看来,select语句每执行一次是遍历整张表,举个例子:这样就是在求200802季度的累计的时候也遍历了整张表,而我想得到的是在200803以后的值就不在遍历了,跳过对下边表的遍历求200802 dw2的累计值
      

  10.   

    18楼的两个select语句可不可以合在一起?