此语句将生成一个4列表:
SELECT shipped.badguy AS badguy, shipped.sdate AS LineDate,
'Delivery' AS Legend, -price*quantity AS amount
FROM product JOIN shipped ON (product.id = shipped.product)
UNION
SELECT receipt.badguy, receipt.rdate,notes, amount
FROM receipt
如何获得上述产生的第4列的总和?
答案 0 :(得分:1)
SUM(-price*quantity)
啊?
答案 1 :(得分:1)
尝试
sum(price*quantity)
或将两个查询合并为
SELECT badguy,rdate,notes,SUM(AMOUNT) FROM(
SELECT shipped.badguy AS badguy, shipped.sdate AS LineDate,
'Delivery' AS Legend, -price*quantity AS amount
FROM product JOIN shipped ON (product.id = shipped.product)
UNION
SELECT receipt.badguy, receipt.rdate,notes, amount
FROM) A
答案 2 :(得分:1)
我喜欢使用子查询。
SELECT SUM(results.amount)
FROM
(
SELECT -price*quantity AS amount
FROM product JOIN shipped ON (product.id = shipped.product)
UNION
SELECT amount
FROM
...
) results
答案 3 :(得分:1)
将查询包装在另一个查询中,外部查询只获取所需的总和。这使您的查询成为子查询。
SELECT SUM(amount) FROM (
SELECT shipped.badguy AS badguy, shipped.sdate AS LineDate,
'Delivery' AS Legend, -price*quantity AS amount
FROM product JOIN shipped ON (product.id = shipped.product)
UNION
SELECT receipt.badguy, receipt.rdate,notes, amount
FROM <...>
)