正如标题所述,我想将两个值彼此分开,这两个值在同一列中.
例如.
例如.
A B C D Shirts 2011 85 0 Shirts 2012 92 percent change from 2011 to 2012 Shirts 2013 100 percent change from 2012 to 2013 Pants 2011 31 0 Pants 2012 42 percent change from 2011 to 2012 Pants 2013 55 percent change from 2012 to 2013 Jacket 2011 10 0 Jacket 2012 16 percent change from 2011 to 2012 Jacket 2013 18 percent change from 2012 to 2013
在此示例中,列D将从列C派生,其中2012的值从2011中减去,然后乘以100以获得百分比.
解决方法
这是一个选择,让你得到你想要的东西
SELECT t1.a,t1.b,t1.c,CASE WHEN t2.c IS NULL THEN 0 ELSE t1.c - t2.c END AS d,CASE WHEN t2.c IS NULL THEN 0 ELSE (1.0 * t1.c - t2.c) / t1.c * 100.0 END AS pct FROM t t1 LEFT OUTER JOIN t t2 ON t1.a = t2.a AND t1.b = t2.b + 1