T1:编号         时间                      金额          进程
1001        2011-01-01 12:31:23         100           0
1001        2011-01-02 12:31:23         200           0
1001        2011-01-03 12:31:23         300           2
1001        2011-01-04 12:31:23         400           0
1002        2011-01-02 12:31:23         100           0
1002        2011-01-03 12:31:23         200           1
1002        2011-01-04 12:31:23         300           0
1002        2011-01-05 12:31:23         400           0
1002        2011-01-08 12:31:23         500           0
1002        2011-01-10 12:31:23         600           0
1003        2011-01-21 12:31:23         200           0
1003        2011-01-22 12:31:23         200           0
1003        2011-01-23 12:31:23         400           0
1003        2011-01-24 12:31:23         600          10
1003        2011-01-25 12:31:23         800           0
结果编号          前2天金额合计         进程不为0的金额合计          进程不为0以后金额合计(不含不为0的那天)
1001            300                      300                              400
1002            100                      200                              1800
1003            400                      600                              800注意的地方:如果进程不为0的前面天数不足2天,但不足的合计(如果一天都没有,那就是0,有一天就算一天),超过2天得按第一和第二天算

解决方案 »

  1.   


    declare @T1 table (编号 int,时间 datetime,金额 int,进程 int)
    insert into @T1
    select 1001,'2011-01-01 12:31:23',100,0 union all
    select 1001,'2011-01-02 12:31:23',200,0 union all
    select 1001,'2011-01-03 12:31:23',300,2 union all
    select 1001,'2011-01-04 12:31:23',400,0 union all
    select 1002,'2011-01-02 12:31:23',100,0 union all
    select 1002,'2011-01-03 12:31:23',200,1 union all
    select 1002,'2011-01-04 12:31:23',300,0 union all
    select 1002,'2011-01-05 12:31:23',400,0 union all
    select 1002,'2011-01-08 12:31:23',500,0 union all
    select 1002,'2011-01-10 12:31:23',600,0 union all
    select 1003,'2011-01-21 12:31:23',200,0 union all
    select 1003,'2011-01-22 12:31:23',200,0 union all
    select 1003,'2011-01-23 12:31:23',400,0 union all
    select 1003,'2011-01-24 12:31:23',600,10 union all
    select 1003,'2011-01-25 12:31:23',800,0;with maco as 
    (
    select row_number() over (partition by 编号 order by 时间) as id,
    * from @T1
    )select 
    编号,
    (select sum(金额) from maco where 编号=a.编号 and id<3) as 前2天金额合计,
    (select sum(金额) from maco where 编号=a.编号 and 进程<>0) as 进程不为0的金额合计,
    (select sum(金额) from  maco
    where 编号=a.编号 and 时间>
    (select min(时间) from maco where 编号=a.编号 and 进程<>0)) as 进程不为0以后金额合计
    from maco a group by 编号
    /*
    编号          前2天金额合计     进程不为0的金额合计  进程不为0以后金额合计
    ----------- ----------- ----------- -----------
    1001        300         300         400
    1002        300         200         1800
    1003        400         600         800
    */
      

  2.   

    declare @T1 table (编号 int,时间 datetime,金额 int,进程 int)
    insert into @T1
    select 1001,'2011-01-01 12:31:23',100,0 union all
    select 1001,'2011-01-02 12:31:23',200,0 union all
    select 1001,'2011-01-03 12:31:23',300,2 union all
    select 1001,'2011-01-04 12:31:23',400,0 union all
    select 1002,'2011-01-02 12:31:23',100,0 union all
    select 1002,'2011-01-03 12:31:23',200,1 union all
    select 1002,'2011-01-04 12:31:23',300,0 union all
    select 1002,'2011-01-05 12:31:23',400,0 union all
    select 1002,'2011-01-08 12:31:23',500,0 union all
    select 1002,'2011-01-10 12:31:23',600,0 union all
    select 1003,'2011-01-21 12:31:23',200,0 union all
    select 1003,'2011-01-22 12:31:23',200,0 union all
    select 1003,'2011-01-23 12:31:23',400,0 union all
    select 1003,'2011-01-24 12:31:23',600,10 union all
    select 1003,'2011-01-25 12:31:23',800,0;with maco as 
    (
    select row_number() over (partition by 编号 order by 时间) as id,
    * from @T1
    )select 
        编号,
        (select sum(金额) from maco where 编号=a.编号 and id<3 
        and 时间<(select min(时间) from maco where 编号=a.编号 and 进程<>0)) as 前2天金额合计,
        (select sum(金额) from maco where 编号=a.编号 and 进程<>0) as 进程不为0的金额合计,
        (select sum(金额) from  maco
        where 编号=a.编号 and 时间>
        (select min(时间) from maco where 编号=a.编号 and 进程<>0)) as 进程不为0以后金额合计
    from maco a group by 编号/*
    编号          前2天金额合计     进程不为0的金额合计  进程不为0以后金额合计
    ----------- ----------- ----------- -----------
    1001        300         300         400
    1002        100         200         1800
    1003        400         600         800
    */
      

  3.   

      可否解释下 select row_number() over (partition by 编号 order by 时间) 谢谢!!
      

  4.   

    partition by 编号 按编号分组,然后再分别生成每组的序号。