I'd like to get sum of column1, sum of column2 and total sum. In Postgres I can do it this way: (notice 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 an syntax error and have to use this:
SELECT a,b, a+b AS total_sum FROM ( SELECT SUM(column1) AS a, SUM(column2) AS b FROM table )
I have really lot of columns to return so I do not want to write the column names again in the main query. Is there any easy solution?
I cannot use a+b in the inner query because there are not known at this place. I do not want to use SELECT SELECT SUM(column1) AS a, SUM(column2) AS b, SUM(column1)+SUM(column2) AS total_sum
.
An asterisk (" * ") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: The FROM clause, which indicates the table(s) to retrieve data from.
The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.
A view is created referencing a specific column in a source table and the column is subsequently dropped from the table. A view is created using SELECT * from a table and any column is subsequently dropped from the table.
(Entry 1 of 3) 1 : chosen from a number or group by fitness or preference. 2a : of special value or excellence : superior, choice. b : exclusively or fastidiously chosen often with regard to social, economic, or cultural characteristics.
You can select every column from that sub-query by aliasing it and adding the alias before the *
:
SELECT t.*, a+b AS total_sum FROM ( SELECT SUM(column1) AS a, SUM(column2) AS b FROM table ) t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With