我的表中的记录是像下面如何计算记录之间的差异?
ym cnt
200901 57
200902 62
200903 67
...
201001 84
201002 75
201003 75
...
201101 79
201102 77
201103 80
...
我想电脑当前的月份每月之间以及差异。 结果想下面......
ym cnt diff
200901 57 57
200902 62 5 (62 - 57)
200903 67 5 (67 - 62)
...
201001 84 ...
201002 75
201003 75
...
201101 79
201102 77
201103 80
...
谁能告诉我如何写一个SQL来得到结果,并且有着不错的表现?
UPDATE:
对不起简单的话
我的解决办法是
step1: input the currentmonth data into temp table1
step2: input the permonth data into temp table2
step3: left join 2 tables to compute the result
Temp_Table1
SELECT (ym - 1) as ym , COUNT(item_cnt) as cnt
FROM _table
GROUP BY (ym - 1)
order by ym
Temp_Table2
SELECT ym , COUNT(item_cnt) as cnt
FROM _table
GROUP BY ym
order by ym
select ym , (b.cnt - a.cnt) as diff from Temp_Table2 a
left join Temp_Table1 b
on a.ym = b.ym
* 如果我要比较今年和去年 月间差异,我只能改变YM - 1至YM - 100 *
但实际上,该组来者皆是不仅YM
有最多15个按键和最大100百万记录
所以,我不知道一个很好的解决方案可以轻松管理源
和良好的表现。
可能的问题,如[这里](HTTP ://stackoverflow.com/questions/7794590/how-to-version-dynamic-business-objects-data)可能会帮助你 – linuxeasy