Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select * from subquery

Tags:

sql

oracle

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.

like image 871
karel Avatar asked Jan 18 '12 14:01

karel


People also ask

What is select a * in SQL?

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.

Why we use select * from in SQL?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.

Can we use select * In view?

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.

What select * means?

(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.


1 Answers

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 
like image 184
Peter Lang Avatar answered Sep 20 '22 14:09

Peter Lang