尝试从列中获得累积总和,我将在运行中求和

时间:2015-05-05 14:26:23

标签: mysql cumulative-sum

我有一张下表 -

+------------------------------------------+
¦ date       ¦ earn_points ¦ redeem_points ¦
¦------------+-------------+---------------¦
¦ 2015-05-05 ¦ 50          ¦ 30            ¦
¦------------+-------------+---------------¦
¦ 2015-05-05 ¦ 60          ¦ 30            ¦
¦------------+-------------+---------------¦
¦ 2015-05-04 ¦ 70          ¦ 50            ¦
¦------------+-------------+---------------¦
¦ 2015-05-04 ¦ 80          ¦ 40            ¦
¦------------+-------------+---------------¦
¦ 2015-05-03 ¦ 30          ¦ 20            ¦
+------------------------------------------+

我正在寻找以下结果 -

+-------------------------------------------------------------------------------------------------------------+
¦ date       ¦ total_earn_points ¦ total_redeem_points ¦ total_liability_points ¦ Cumulative_liability_points ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-05 ¦ 110               ¦ 60                  ¦ 50                     ¦ 120                         ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-04 ¦ 150               ¦ 90                  ¦ 60                     ¦ 70                          ¦
¦------------+-------------------+---------------------+------------------------+-----------------------------¦
¦ 2015-05-03 ¦ 30                ¦ 20                  ¦ 10                     ¦ 10                          ¦
+-------------------------------------------------------------------------------------------------------------+

我正在尝试此SQL查询,但无法获得正确的累计总数:

SELECT `transaction_date`, 
IFNULL(SUM(rewards_point_rewarded),0) AS `total_earn_points`, 
IFNULL(SUM(rewards_point_redemed),0) AS `total_redeem_points`, 
(SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `total_liability_points`, 
@total := @total + (SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `Cumulative_liability_points` 
FROM `i_report_total_order`, (SELECT @total:=0) AS t 
WHERE (website_id = '36') 
GROUP BY `transaction_date` 
ORDER BY `transaction_date` DESC

请帮助达到预期效果。

2 个答案:

答案 0 :(得分:0)

忽略亚历山大的评论,你显然已经走了,结构稳固,人们总是可以选择不回答。

这实际上是雷区中使用@变量的@变量累积的雷区。

我首先采用获取非累积量的方法,然后使用@变量找到解决方案:

  input = MeanShift.clone();
  input.convertTo(input, CV_32F);

  for(int i = 0; i < Pyramid_Size; i++){DS_Pyramid[i] = input.clone();}

  for (int i = 0; i < Pyramid_Size; i++){ 
    for (int k = 0; k <= i; k++){ // Why don't I just downsamplex3 a copy of MeanShift.clone then upsamplex3 that same one? ...
      pyrDown (DS_Pyramid[i], DS_Pyramid[i], Size(DS_Pyramid[i].cols/2, DS_Pyramid[i].rows/2));
      US_Pyramid[i] = DS_Pyramid[i].clone();
    }
    for (int j = 0; j <= i; j++){
      pyrUp (US_Pyramid[i], US_Pyramid[i], Size(US_Pyramid[i].cols*2, US_Pyramid[i].rows*2));
    }
  }

  top = US_Pyramid[Pyramid_Size - 1].clone(); // most down sampled layer, up sampled.
  split(top, top_chs);
  split(input.clone(), meanShift_chs); // split into channels result
  split(input.clone(), sal_chs); // holder to use for compare

  float top_min = 1.0;
  float ms_min = 1.0;
  for (int i = 0; i < top.rows; i++){   // find the smallest value in both top and meanShift
    for (int k = 0; k < top.cols; k++){ // this is so you can sub out the 0 with the min value
      for (int j = 0; j < top.channels(); j++){ // later on
    float a = top_chs[j].at<float>(i,k);
    float b = meanShift_chs[j].at<float>(i,k);
    if (a < top_min && a >= 0) {top_min = a;} // make sure you don't have a top_min of zero... that'd be bad.
    if (b < ms_min && b >= 0)  { ms_min = b;}
      }
    }
  }

  for (int i = 0; i < top.rows; i++){
    for (int k = 0; k < top.cols; k++){
      for (int j = 0; j < top.channels(); j++){
    float a,b,c;
    a = top_chs[j].at<float>(i,k);
    b = meanShift_chs[j].at<float>(i,k);

    if (a <= 0){a = top_min;} // make sure you don't divide by zero
    if (b <= 0){b = ms_min;} // make sure you really don't divide by zero
    if (a <= b){c = 1.0 - a/b;}
    else {c = 1.0 - b/a;}

    // c = sqrt(c); // makes stuff more salient, but makes noise pop out too
    sal_chs[j].at<float>(i,k) = c;
      }
    }
  }
  merge(sal_chs, Saliency); // combine into saliency map
  imshow("saliency", Saliency);

然后:

    MeanShift = inputImage.clone();

    Imgproc.pyrMeanShiftFiltering(MeanShift, MeanShift, MeanShift_spatialRad, MeanShift_colorRad);
    Imgproc.cvtColor(MeanShift, MeanShift, Imgproc.COLOR_BGR2GRAY);

    MeanShift.convertTo(MeanShift, CvType.CV_32F);                 // 32F between 0 - 1. ************** IMPORTANT LINE

    for (int i = 0; i < PyrSize; i++){
        DS_Pyramid.add(new Mat());
        UP_Pyramid.add(new Mat());
    }

    for (int i = 0; i < PyrSize; i++){    
        DS_Pyramid.set(i, MeanShift);
    }       

    for (int i = 0; i < PyrSize; i++){
        for(int k = 0; k <= i; k++){                               // At 0 is downsampled once, second twice, third 3 times. 
        Imgproc.pyrDown(DS_Pyramid.get(i), DS_Pyramid.get(i)); // pyrDown by default img.width / 2 img height / 2
        Mat a = new Mat();                                     // save the sampled down at i
        a = DS_Pyramid.get(i);                            
        UP_Pyramid.add(a);
        }
        for (int j = 0; j <= i; j++){
        Imgproc.pyrUp(UP_Pyramid.get(i),UP_Pyramid.get(i));    
        }                                                          
    }
    top = UP_Pyramid.get(PyrSize-1);
    bot = MeanShift.clone();
    Saliency = MeanShift.clone();


    //http://answers.opencv.org/question/5/how-to-get-and-modify-the-pixel-of-mat-in-java/
    //http://www.tutorialspoint.com/java_dip/applying_weighted_average_filter.htm

    for (int i = 0; i < top.rows(); i++){
        for (int j = 0; j < top.cols(); j++){
        int index = i * top.rows() + j;

        float[] top_temp = top.get(i, j);
        float[] bot_temp  = bot.get(i,j);
        float[] sal_temp = bot.get(i,j);

        if (top_temp[0] <= bot_temp[k]){sal_temp[0] = 1.0f - (top_temp[0]/bot_temp[0]);}
        else                           {sal_temp[0] = 1.0f - (bot_temp[0]/top_temp[0]);}

        Saliency.put(i,j, sal_temp);
        }
    }

    AbstractImageProvider.deepCopy(AbstractImageProvider.matToBufferedImage(Saliency),disp);

不幸的是,您必须再次换行以获取您指定的日期顺序..但您可以通过将列设置为 SELECT date, SUM(earn_points) AS tot_earn_pts, SUM(redeem_points) AS tot_redeem_pts FROM i_report_total_order /* WHERE website_id = 36 */ GROUP BY date ORDER BY date DESC 来避免 SELECT date, tot_earn_pts, tot_redeem_pts, tot_earn_pts - tot_redeem_pts AS tot_liability_pts, @cum := @cum + tot_earn_pts - tot_redeem_pts AS cum_liability_pts FROM ( SELECT date, SUM(earn_points) AS tot_earn_pts, SUM(redeem_points) AS tot_redeem_pts FROM i_report_total_order /* WHERE website_id = 36 */ GROUP BY date ) tots JOIN (SELECT @cum := 0) init ORDER BY date

请参阅SQLFiddle Example

<强>更新

IFNULLS

更新了SQLFiddle

答案 1 :(得分:0)

根据您的帮助,这是我的案例中的查询

SELECT `transaction_date`, 
       `total_earn_points`, 
       `total_redeem_points`, 
       `total_liability_points`, 
       @total := @total + `total_liability_points` AS `Cumulative_liability_points` 
       FROM (
              SELECT `transaction_date`, 
              SUM(rewards_point_rewarded) AS `total_earn_points`, 
              SUM(rewards_point_redemed) AS `total_redeem_points`,
              (SUM(rewards_point_rewarded) - SUM(rewards_point_redemed)) AS `total_liability_points`  
              FROM `i_report_total_order` 
              WHERE (website_id = '36') 
              GROUP BY `transaction_date` 
              ORDER BY `transaction_date` ASC
       ) tots 
join (SELECT @total:=0) AS init 
ORDER BY `transaction_date` ASC

只有在最后一行设置ASC时才有效。有没有办法在最上面显示最新日期。