如何添加行值?

时间:2013-02-26 11:42:23

标签: oracle oracle10g oracle11g

DATE       PCODE         ACODE             TRAFFIC      DIFFERENCE
25-02-2013  S_HDFCBANK  S_HDFCBANK     15085056
24-02-2013  S_HDFCBANK  S_HDFCBANK     9118809

请帮助我获取两个日期的流量差异并添加到最后一列。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

SQL> select dte,pcode,acode,traffic,
  2         traffic - lag(traffic) over (order by dte) difference
  3    from data
  4   order by 1, 2, 3;

DTE       PCODE      ACODE         TRAFFIC DIFFERENCE
--------- ---------- ---------- ---------- ----------
24-FEB-13 S_HDFCBANK S_HDFCBANK    9118809
25-FEB-13 S_HDFCBANK S_HDFCBANK   15085056    5966247

如果您每个日期有多个pcode acode,并希望单独处理这些,则需要将分析更改为

lag(traffic) over (partition by pcode,acode order by dte) difference