SQL计算月份百分比

时间:2015-06-15 22:51:43

标签: sql sql-server-2008-r2

我正在使用SQL Server 2008R2。我正在尝试计算以下数据的百分比格式的月比例变化:

uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {
  // x and y aren't used, so you can remove those from the above signature too.
  uchar4 out;
  float3 pixel = convert_float4(in).rgb;

  pixel.r = (pixel.r + pixel.g + pixel.b)/3;
  // This seems buggy to me below, since pixel.r was just modified.
  // I think you need another temporary variable (assuming you are trying to make this work and getting weird behavior).
  pixel.g = (pixel.r + pixel.g + pixel.b)/3;
  pixel.b = (pixel.r + pixel.g + pixel.b)/3;

  //int topRight
  //float4 f4 = rsUnpackColor8888(*(uchar*)rsGetElementAt(inPixels, x+1, y+1));

  out.xyz = convert_uchar3(pixel);
  return out;
}

Excel中使用的上一个公式是=(b2-b1)/ b1。我正在努力实现这一目标:

Month      TransCnt
2015-01-01 2830812
2015-02-01 2760239
2015-03-01 3313988
2015-04-01 3355073
2015-05-01 3599517

1 个答案:

答案 0 :(得分:1)

您需要获取之前的值。好吧,最简单的事情是升级SQL Server并使用lag()函数;)哦,也许这不是一个选项。

这是等效的:

select d.*,
       (1 - d2.transcnt * 1.0 / d.transcnt) as monthly_change
from data d outer apply
     (select top 1 d2.*
      from data d2
      where d2.month < d.month
      order by month desc
     ) dprev;