在Oracle查询中减去两个数字列值

时间:2016-07-31 02:46:34

标签: sql oracle

我需要减去两个列值并在select查询中显示输出列。

SELECT c.name,
    (SELECT SUM(p.qty*s.price)
     FROM stock s, purchase p 
     WHERE s.company = p.company 
     GROUP BY p.company) 
 MINUS 
    (SELECT SUM(p.price*p.qty) 
     FROM stock s, purchase p 
     WHERE s.company = p.company 
     GROUP BY p.company)  AS "Profit/Lost" 
 FROM purchase p, client c, stock s 
 WHERE  s.company = p.company 
     AND c.clno = p.clno  
 GROUP BY p.company;

但它会显示错误。怎么解决?

1 个答案:

答案 0 :(得分:5)

第一个子查询需要一个别名。

但为什么要使用子查询呢? 这是你想要的吗?

SELECT c.name, p.company,
   SUM(p.qty*s.price) - SUM(p.price*p.qty) roe
FROM client c
   join purchase p on p.clno  = c.clno 
   join stock s on s.company = p.company 
GROUP BY c.name, p.company;