SELECT Table1.NUMBER_ID,
Table1.ACCOUNT,
Table1.NAME_CODE,
Table1.TIME,
Table1.DATE,
Table2.UNIT_LABEL,
Table1.TIME - NVL(LAG ( Table1.TIME ) OVER ( ORDER BY Table1.TIME ), Table1.TIME) diff
FROM Table2
INNER JOIN Table1
ON Table2.NAME = Table1.NAME_CODE
WHERE DATE >= trunc(sysdate - 1)
AND UNIT_LABEL = 'KG'
ORDER BY
Table1.NAME_CODE,
Table1.TIME
您好, 我有上面的SQL代码,它允许我获得单位标签“KG”的项目的购买时间差异。我希望在每个NAME_CODE中获取购买时间之间的差异,因此我想在执行计算之前通过NAME_CODE订购以获得购买时间的滞后差异。
我的PL / SQL代码从Oracle获取信息。此代码目前正在执行延迟时差计算,然后对不是我想要的数据进行排序。我首先需要通过NAME_CODE对数据进行排序,然后在购买时间中获得滞后,这样我就可以按照NAME_CODE使用它,而不是使用UNIT_LABEL为“KG”的所有内容
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
使用PARTITION BY
analytic function中的LAG
条款:
SELECT Table1.NUMBER_ID,
Table1.ACCOUNT,
Table1.NAME_CODE,
Table1.TIME,
Table1.DATE,
Table2.UNIT_LABEL,
Table1.TIME - NVL(LAG ( Table1.TIME ) OVER ( PARTITION BY Table1.NAME_CODE ORDER BY Table1.TIME ), Table1.TIME) diff
FROM Table2
INNER JOIN Table1
ON Table2.NAME = Table1.NAME_CODE
WHERE DATE >= trunc(sysdate - 1)
AND UNIT_LABEL = 'KG'
ORDER BY
Table1.NAME_CODE,
Table1.TIME