SQL 中两个查询之间的差异(Oracle Apex 项目)

发布于 2025-01-12 05:39:03 字数 537 浏览 0 评论 0原文

我在 APEX 应用程序的不同项目中有以下两个查询:


ITEM 1:

SELECT SUM(t.gems)
  FROM (SELECT gems
          FROM tasks
         UNION ALL
        SELECT gems
          FROM quests
         UNION ALL
        SELECT gems
          FROM daily_quests
         ) t

ITEM 2:

SELECT SUM(price) FROM items

它们都正确返回一个数字,但我想在一个语句中减去它们。我尝试使用

SELECT TO_NUMBER(ITEM1)-TO_NUMBER(ITEM2) FROM DUAL

但没有成功。

您有什么建议吗?我对 APEX 和 SQL 有点陌生。

先感谢您。

I have the two following queries in different items in an APEX application:


ITEM 1:

SELECT SUM(t.gems)
  FROM (SELECT gems
          FROM tasks
         UNION ALL
        SELECT gems
          FROM quests
         UNION ALL
        SELECT gems
          FROM daily_quests
         ) t

ITEM 2:

SELECT SUM(price) FROM items

They both return a number correctly, but I want to subtract them in one statement. I tried to use

SELECT TO_NUMBER(ITEM1)-TO_NUMBER(ITEM2) FROM DUAL

but it didn't work.

Do you have any suggestions? I am a bit new to APEX and SQL.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

对不⑦ 2025-01-19 05:39:04

假设每个查询生成一个名为 gems 的列,您可以执行以下操作:

with
a as (
  -- query 1 here
),
b as (
  -- query 2 here
)
select a.gems - b.gems as diff
from a
cross join b

Assuming each query produces a column named gems you can do:

with
a as (
  -- query 1 here
),
b as (
  -- query 2 here
)
select a.gems - b.gems as diff
from a
cross join b
掀纱窥君容 2025-01-19 05:39:04

Koen 的答案给出了此类问题的一般正确答案。

但是,在您的特定实例中,您可以使用更简单的查询:

SELECT SUM(t.gems)
FROM (SELECT gems FROM tasks
      UNION ALL
      SELECT gems FROM quests
      UNION ALL
      SELECT gems FROM daily_quests
      UNION ALL
      SELECT price * -1 FROM items
) t

Koen's answer gives the general, correct answer for this type of problem.

In your particular instance, however, you can use a simpler query:

SELECT SUM(t.gems)
FROM (SELECT gems FROM tasks
      UNION ALL
      SELECT gems FROM quests
      UNION ALL
      SELECT gems FROM daily_quests
      UNION ALL
      SELECT price * -1 FROM items
) t
夏末 2025-01-19 05:39:03

对每个查询使用 CTE(公共表表达式),然后CROSS JOIN 它们。

WITH t_gems (sum_gems) AS
(
SELECT SUM(t.gems)
  FROM (SELECT gems
          FROM tasks
         UNION ALL
        SELECT gems
          FROM quests
         UNION ALL
        SELECT gems
          FROM daily_quests
         ) t
), t_items (sum_price) AS
(
  SELECT SUM(price) FROM items
)
SELECT t_gems.sum_gems - t_item.sum_price 
  FROM t_gems CROSS JOIN t_items

Use a CTE (Common table expression) for each of the queries, then CROSS JOIN them.

WITH t_gems (sum_gems) AS
(
SELECT SUM(t.gems)
  FROM (SELECT gems
          FROM tasks
         UNION ALL
        SELECT gems
          FROM quests
         UNION ALL
        SELECT gems
          FROM daily_quests
         ) t
), t_items (sum_price) AS
(
  SELECT SUM(price) FROM items
)
SELECT t_gems.sum_gems - t_item.sum_price 
  FROM t_gems CROSS JOIN t_items

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