我想加入这两个查询,我想从 PQuantity 中减去 SQuantity,从 PAmount 中减去 SAmount。我怎样才能通过一个查询来实现这一点。
SELECT
SUM(QtyShare) AS SQuantity, SUM(QtyShare * Rate) AS SAmount,CompanyId
FROM Sales
WHERE (UserId = 1)
GROUP BY CompanyId
SELECT SUM(Quantity) AS PQuantity, SUM(Quantity * Rate) AS PAmount,CompanyId
FROM (SELECT UserId, CompanyId, Quantity, Rate AS Rate
FROM Purchase
UNION ALL
SELECT UserId, CompanyId, Quantity, 0 AS Rate
FROM Bonus
UNION ALL
SELECT UserId, CompanyId, Quantity, Rate AS Rate
FROM RightShare) AS t
WHERE (UserId = 1) GROUP BY CompanyId