0

我有一个表 a_daily 有像这样的列

1.date
2.FromDate
3.Target
4.Cumulative_Target

我将每天添加行,因此每天参考 fromdate 和 date Cumulative_Target 列将更新,如表中所示

这是我需要做的:

    date                Fromdate      Target          Cumulative_Target
    2012-04-16    2012-04-16    500              500                  '(cycle1-started)
    2012-04-17    2012-04-16    500              1000    
    2012-04-18    2012-04-16    800              1800 
    '
    '
    '
    2012-05-15   2012-04-16     200             xxxxxx                 '(cycle1-completed)
    2012-05-16   2012-05-16     150             150                     '(cycle2-started)
    2012-05-17   2012-05-16     100             250
    2012-05-18   2012-05-16     700             950
    '
    '
    '
    '
    2012-06-15  2012-06-15      200             xxxxx                    '(Cycle2-completed)

所以表中的起始日期将在每个周期内变化

我用于更新的查询是:

UPDATE a_daily 
SET a_daily.Cumulative_Generation = sum(a_daily.Target) 
    between a_daily.Date and a_daily.From_Date

我不知道如何化妆计算列请帮助我!

4

2 回答 2

1

试试这个查询 -

SET @count = 0;
UPDATE a_daily
  SET Cumulative_Target = @count:= target + @count
ORDER BY date

...如果需要,请指定您的订单条款

于 2012-11-21T08:16:48.073 回答
1
UPDATE TableName a
       INNER JOIN 
        (
            SELECT  DATE(a.date) date, 
                    date(a.Fromdate) fromdate,
                    a.target,
                     @accum:=@accum + a.runningTotal AS TOTAL
            FROM 
                (  
                  SELECT  DATE(date) date, 
                          date(Fromdate)Fromdate , 
                          target,
                          SUM(`Target`) AS runningTotal
                  FROM TableName
                  GROUP BY DATE(date), date(Fromdate),target
                  ORDER BY Fromdate
                ) a , (SELECT @accum := 0) s

        ) b ON DATE(a.Date) = b.date AND
               DATE(a.FROMDATE) = b.fromdate AND
              a.target = b.target
SET a.Cumulative_Target = b.total
于 2012-11-21T08:09:56.757 回答