查找 2 个特定行之间的差异
我需要找到 2 个特定行之间的差异,但无法破解 LAG/LEAD 函数,并且不确定这些函数是否有帮助。
需要从dest=OM-OM_20的行中减去dest=OM-OM_225的行。
使用Oracle 9i。
表是使用以下命令创建的:
SELECT TRUNC(DATETIME,'HH') DATETIME,decode(OBJECT_ID,20,'OM-OM_20',225,'OM-OM_225',250,'OM-PSTN','OM-INT') DEST,
sum(BO) as CAABS,
SUM(BA+ RE) as CATT,
round(SUM((AC/22)*11,2) as CAMIN
FROM SCHEMA.TABLE
WHERE ((OBJECT_ID = 20) or (OBJECT_ID = 225) or (OBJECT_ID = 250) or (OBJECT_ID = 150) or (OBJECT_ID =160) or (OBJECT_ID = 161) or (OBJECT_ID = 162) or (OBJECT_ID = 163) or (OBJECT_ID = 164) or (OBJECT_ID = 165) or(OBJECT_ID = 166) or (OBJECT_ID = 167) )
and DATETIME between trunc(sysdate,'hh')-1/24 and trunc(sysdate,'hh')-1/24/3600
group by TRUNC(DATETIME,'HH'), decode(OBJECT_ID,20,'OM-OM_20',225,'OM-OM_225',250,'OM-PSTN','OM-INT')
order by 1 desc
OUTPUT
DATETIME DEST CAABS CATT CAMINS 9/7/2009 1:00:00 PM OM-INT 10417 64670 87971.67 9/7/2009 1:00:00 PM OM-PSTN 7372 95388 13309.17 9/7/2009 1:00:00 PM OM-OM_20 6767 231884 184952.5 9/7/2009 1:00:00 PM OM-OM_225 33104 101003 68570.83
I need to find the difference between 2 specific rows but cannot hack the LAG/LEAD functions and not sure if these functions can help.
The row where dest=OM-OM_225 needs to be subtracted from the row where dest=OM-OM_20.
Using Oracle 9i.
Table is created using:
SELECT TRUNC(DATETIME,'HH') DATETIME,decode(OBJECT_ID,20,'OM-OM_20',225,'OM-OM_225',250,'OM-PSTN','OM-INT') DEST,
sum(BO) as CAABS,
SUM(BA+ RE) as CATT,
round(SUM((AC/22)*11,2) as CAMIN
FROM SCHEMA.TABLE
WHERE ((OBJECT_ID = 20) or (OBJECT_ID = 225) or (OBJECT_ID = 250) or (OBJECT_ID = 150) or (OBJECT_ID =160) or (OBJECT_ID = 161) or (OBJECT_ID = 162) or (OBJECT_ID = 163) or (OBJECT_ID = 164) or (OBJECT_ID = 165) or(OBJECT_ID = 166) or (OBJECT_ID = 167) )
and DATETIME between trunc(sysdate,'hh')-1/24 and trunc(sysdate,'hh')-1/24/3600
group by TRUNC(DATETIME,'HH'), decode(OBJECT_ID,20,'OM-OM_20',225,'OM-OM_225',250,'OM-PSTN','OM-INT')
order by 1 desc
OUTPUT
DATETIME DEST CAABS CATT CAMINS 9/7/2009 1:00:00 PM OM-INT 10417 64670 87971.67 9/7/2009 1:00:00 PM OM-PSTN 7372 95388 13309.17 9/7/2009 1:00:00 PM OM-OM_20 6767 231884 184952.5 9/7/2009 1:00:00 PM OM-OM_225 33104 101003 68570.83
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果想在同一个select中执行此操作,可以使用join:(
查询的具体形式可以根据DB而不同)
If you want to do this in the same select, you can use joins:
(The concrete form of the query can be different depending on DB)