Select * from subquery

I would like to get the sum of column1, the sum of column2 and the total amount. In Postgres, I can do it like this: (pay attention to the star)

SELECT *, a+b AS total_sum FROM ( SELECT SUM(column1) AS a, SUM(column2) AS b FROM table ) 

But in Oracle, I get a syntax error and should use this:

 SELECT a,b, a+b AS total_sum FROM ( SELECT SUM(column1) AS a, SUM(column2) AS b FROM table ) 

I really have a lot of columns to return, so I don't want to write the column names in the main query again. Is there a simple solution?

I cannot use a + b in an internal query because it is not known there. I do not want to use SELECT SELECT SUM(column1) AS a, SUM(column2) AS b, SUM(column1)+SUM(column2) AS total_sum .

+50
sql oracle
Jan 18 2018-12-18T00:
source share
1 answer

You can select each column from this subquery by overlaying it with an alias and adding an alias before * :

 SELECT t.*, a+b AS total_sum FROM ( SELECT SUM(column1) AS a, SUM(column2) AS b FROM table ) t 
+106
Jan 18 2018-12-18T00:
source share



All Articles