Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum of multiple columns in ORACLE

I have a table that has 97 columns, I want to sum 96 columns.

select sum(col1+col2+col3+.....+col96) 
from tableA where meter_id=x;

I do not want to give all 96 column names, what is the best way to do it? Regards, RR

like image 423
user2050018 Avatar asked Feb 07 '13 10:02

user2050018


People also ask

How can I SUM two columns in Oracle?

alter table TABLEA add (MY_TOTAL_COL NUMBER GENERATED ALWAYS AS (col1+col2+col3...) VIRTUAL); Then your query can simply do sum(my_total_col) .

How do you SUM a column in Oracle?

Oracle SUM() function syntaxThe Oracle SUM() function is an aggregate function that returns the sum of all or distinct values in a set of values. The Oracle SUM() function accepts a clause which can be either DISTINCT or ALL . The DISTINCT clause forces the SUM() function to calculate the sum of unique values.

How do I SUM all columns in SQL?

The aggregate function SUM is ideal for computing the sum of a column's values. This function is used in a SELECT statement and takes the name of the column whose values you want to sum. If you do not specify any other columns in the SELECT statement, then the sum will be calculated for all records in the table.

Can you SUM columns in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.


1 Answers

There is no way to avoid writing each column name. All you can do is curse the stupid data modeller and get busy with cut'n'paste.

like image 80
APC Avatar answered Sep 18 '22 14:09

APC