DECLARE @table TABLE
(
id INT,
date DATETIME,
sale FLOAT
)
INSERT INTO @table SELECT 1,'2012-12-01',0 UNION ALL
SELECT 2,'2012-12-02',10 UNION ALL
SELECT 3,'2012-12-03',0 UNION ALL
SELECT 4,'2012-12-04',100 UNION ALL
SELECT 5,'2012-12-05',0 UNION ALL
SELECT 6,'2012-12-06',1000SELECT * FROM @table
WHERE DAY(date)%2=0/*
   想要双号的累加,计算差异
   2号 就是金额10
   4号 就是2号加4号的金额110
   6号 就是2号加4号加6号的金额1110
*/

解决方案 »

  1.   

    select *,
           sale=(select sum(sale) from @table where DAY(date)%2=0 and id<=a.id) 
    from (SELECT * FROM @table WHERE DAY(date)%2=0) a
      

  2.   


    select id,date
    ,sale=(case when day(date)%2=1 then sale else (select sum(sale) from @table where day(date)%2=0 and date<=A.date) end ) 
    from @table as A