SQL ORACLE,差异

SQL ORACLE, Differences

这是代码

 select sum(post_sales) from post_sales;

捐出 50,000

select sum(sales) from sales;

捐出 100,000

我要减去 100,000 - 50,000

Oracle SQL 中的语法是什么?

你可以把这些放在 select:

select ( (select sum(sales) from sales) -
         (select sum(post_sales) from post_sales)
       ) as diff
from dual;

考虑以下几点:

WITH SS AS (SELECT SUM(SALES) AS TOTAL_SALES FROM SALES),
     SPS AS (SELECT SUM(POST_SALES) AS TOTAL_POST_SALES FROM POST_SALES)
SELECT TOTAL_SALES, TOTAL_POST_SALES, TOTAL_SALES - TOTAL_POST_SALES AS SALES_DIFF
  FROM SS
  CROSS JOIN SPS

这里我们使用通用Table表达式SSSPS来计算总销售额和总post销售额。然后我们交叉连接这些 CTE,它们保证每个 return 一行,因此交叉连接保证 return 一行,计算差异,我们就完成了。

dbfiddle here

祝你好运。