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