查找 2 个特定行之间的差异

发布于 2024-08-04 05:54:50 字数 1235 浏览 4 评论 0原文

我需要找到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路终见情 2024-08-11 05:54:50
SELECT  SUM(DECODE(OBJECT_ID, 20, BO, -BO))
FROM    mytable
WHERE   OBJECT_ID IN (20, 225)
        AND DATETIME BETWEEN TRUNC(SYSDATE, 'HH') - 1 / 24 AND TRUNC(SYSDATE, 'hh') - 1 / 24 / 3600
GROUP BY
        TRUNC(DATETIME, 'HH')
SELECT  SUM(DECODE(OBJECT_ID, 20, BO, -BO))
FROM    mytable
WHERE   OBJECT_ID IN (20, 225)
        AND DATETIME BETWEEN TRUNC(SYSDATE, 'HH') - 1 / 24 AND TRUNC(SYSDATE, 'hh') - 1 / 24 / 3600
GROUP BY
        TRUNC(DATETIME, 'HH')
盛夏已如深秋| 2024-08-11 05:54:50

如果想在同一个select中执行此操作,可以使用join:(

select T1.some_field - coalesce(T2.some_field, 0),
...
from TABLE T1
left join TABLE T2 on T2.dest = 'OM-OM_225' and T1.dest = 'OM-OM_20'

查询的具体形式可以根据DB而不同)

If you want to do this in the same select, you can use joins:

select T1.some_field - coalesce(T2.some_field, 0),
...
from TABLE T1
left join TABLE T2 on T2.dest = 'OM-OM_225' and T1.dest = 'OM-OM_20'

(The concrete form of the query can be different depending on DB)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文