我是 SQL 新手,我有 2 个字段,其中一个需要从另一个中减去并显示为总计。我尝试将它放在我的选择语句AS
Total 中,但它引发了一个错误,所以我虽然会尝试Group By
,但效果也不好。如何在 SQL 中将列 Total 作为收入 - 成本?
SELECT PROJECT_NAME, DESCRIPTION, PROJECT_DATE, COST, REVENUE
FROM PROJECT
GROUP BY (REVENUE - COST) AS TOTAL
如果我理解您的要求,您可以使用:
SELECT PROJECT_NAME,
DESCRIPTION,
PROJECT_DATE,
REVENUE,
COST,
(REVENUE - COST) as Total
FROM PROJECT
GROUP BY
如果您尝试聚合数据,将使用。例如,如果您的表为每个项目有多个条目并且您想要一个总数,那么您可以使用:
SELECT PROJECT_NAME,
DESCRIPTION,
PROJECT_DATE,
SUM(REVENUE - COST) as Total
FROM PROJECT
GROUP BY PROJECT_NAME, DESCRIPTION, PROJECT_DATE
你可以试试
SELECT PROJECT_NAME, DESCRIPTION, PROJECT_DATE, COST, REVENUE, ( REVENUE - COST ) profit
FROM PROJECT