Oracle Group By Field - Field

时间:2013-04-23 01:23:14

标签: sql oracle group-by

我是SQL的新手,我有2个字段,其中一个我需要从另一个中减去并显示为Total。我尝试将它放在我的选择语句中AS Total但是它引发了一个错误,所以我虽然尝试了Group By,但这也没有用。如何将列Total作为收入 - SQL中的成本?

SELECT PROJECT_NAME, DESCRIPTION, PROJECT_DATE, COST, REVENUE
FROM PROJECT
GROUP BY (REVENUE - COST) AS TOTAL

2 个答案:

答案 0 :(得分:2)

如果我理解您的请求,您可以使用:

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

答案 1 :(得分:0)

你可以尝试

SELECT PROJECT_NAME, DESCRIPTION, PROJECT_DATE, COST, REVENUE, ( REVENUE - COST ) profit
FROM PROJECT