ORACLE中多列的总和
我有一个有97列的表,我想对96列求和ORACLE中多列的总和,oracle,select,where-clause,Oracle,Select,Where Clause,我有一个有97列的表,我想对96列求和 select sum(col1+col2+col3+.....+col96) from tableA where meter_id=x; 我不想给出所有96个列名,最好的方法是什么? 当做 RR没有办法避免写入每个列名。你所能做的就是诅咒愚蠢的数据建模者,并忙于剪切粘贴。你可以创建一个将96列相加的模型,例如: alter table TABLEA add (MY_TOTAL_COL NUMBER GENERATED ALWAYS AS (col1+c
select sum(col1+col2+col3+.....+col96)
from tableA where meter_id=x;
我不想给出所有96个列名,最好的方法是什么?
当做
RR没有办法避免写入每个列名。你所能做的就是诅咒愚蠢的数据建模者,并忙于剪切粘贴。你可以创建一个将96列相加的模型,例如:
alter table TABLEA add (MY_TOTAL_COL NUMBER GENERATED ALWAYS AS (col1+col2+col3...) VIRTUAL);
然后,您的查询可以简单地进行summay\u total\u col.如果有大量的列,我将研究如何使用数据字典表,通过使用如下查询帮助创建查询:
Select column_name || '+' as column_name_list
From user_tab_columns
Where table_name = 'TABLEA'
Order by column_id
它不会改变世界,但会简化一个查询的编写 最好对列求和,然后将结果放入Excel进行求和。否则,此查询将执行您需要的操作:
SELECT SUM(TOTAL_SUM) FROM (
SELECT SUM(column1) AS TOTAL_SUM FROM your_table
UNION
SELECT SUM(column2) AS TOTAL_SUM FROM your_table
UNION
SELECT SUM(column3) AS TOTAL_SUM FROM your_table
);
有可能:
使用
Mike Meyers的回答是,您可以使用动态sql编写存储过程
sumcolumns(columnfilter,tablename,whereclause)
用它做些类似的事情
select *
from table(sumcolumns('column_name <> ''col97''','tableA','meter_id=x'))
按照下面的示例尝试使用UNPIVOT,但仍需要按照其他人的说明指定列列表:
with tableA as /* prototype tableA just for example */
(
select 1 meter_id, 101 col1, 10 col2, 20 col3, 30 col4, NULL col5, 101 col11, 10 col12, 20 col13, 30 col14, NULL col15, 101 col21, 10 col22, 20 col23, 30 col24, NULL col25 from dual union
select 2, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL from dual union
select 3, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90 from dual union
select 4, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL from dual
)
, unpivoted_tableA as /* UNPIVOT tableA columns into rows */
(
select *
from tableA
unpivot include nulls
(
col_value for col_ in
(COL1,COL2,COL3,COL4,COL5,COL11,COL12,COL13,COL14,COL15,COL21,COL22,COL23,COL24,COL25)
)
)
/* main query - Sum of all columns that were unpivoted to rows */
select meter_id, sum(col_value)
from unpivoted_tableA
group by meter_id
;
OP可以使用dbms_xmlgen.getxml'
with tableA as /* prototype tableA just for example */
(
select 1 meter_id, 101 col1, 10 col2, 20 col3, 30 col4, NULL col5, 101 col11, 10 col12, 20 col13, 30 col14, NULL col15, 101 col21, 10 col22, 20 col23, 30 col24, NULL col25 from dual union
select 2, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL, 102, 40, NULL, 50, NULL from dual union
select 3, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90, 103, 60, 70, 80, 90 from dual union
select 4, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL, 104, 100, NULL, NULL, NULL from dual
)
, unpivoted_tableA as /* UNPIVOT tableA columns into rows */
(
select *
from tableA
unpivot include nulls
(
col_value for col_ in
(COL1,COL2,COL3,COL4,COL5,COL11,COL12,COL13,COL14,COL15,COL21,COL22,COL23,COL24,COL25)
)
)
/* main query - Sum of all columns that were unpivoted to rows */
select meter_id, sum(col_value)
from unpivoted_tableA
group by meter_id
;