select sum(col) from a where month<to_char(sysdate,'YYYY')
假设列名为col_1col_12 select sum(col_value) from ( select 1 col_month, col_1 col_value, col_others from tab_name union all select 2 col_month, col_2 col_value, col_others from tab_name union all select 3 col_month, col_3 col_value, col_others from tab_name union all select 4 col_month, col_3 col_value, col_others from tab_name union all select 5 col_month, col_5 col_value, col_others from tab_name union all select 6 col_month, col_6 col_value, col_others from tab_name union all select 7 col_month, col_7 col_value, col_others from tab_name union all select 8 col_month, col_8 col_value, col_others from tab_name union all select 9 col_month, col_9 col_value, col_others from tab_name union all select 10 col_month, col_10 col_value, col_others from tab_name union all select 11 col_month, col_11 col_value, col_others from tab_name union all select 12 col_month, col_12 col_value, col_others from tab_name ) where ...... and col_month <= to_number(to_char(sysdate, 'mm')) ;
select sum(val) from a where month<=to_char(sysdate,'mm')
SELECT SUM(VAL) FROM A WHERE MONTH <= TO_CHAR(SYSDATE, 'MM');
首先把列名改一下,不知妥否我觉得用数字作列名怪怪的,不妨依次叫col1到col12select sum(col_value) as total from ( select col1 as col_value,'1' as flag from mytable union select col2 as col_value,'2' as flag from mytable union select col3 as col_value,'3' as flag from mytable union select col4 as col_value,'4' as flag from mytableunion select col5 as col_value,'5' as flag from mytable union select col6 as col_value,'6' as flag from mytable union select col7 as col_value,'7' as flag from mytable union select col8 as col_value,'8' as flag from mytableunion select col9 as col_value,'9' as flag from mytable union select col10 as col_value,'10' as flag from mytable union select col11 as col_value,'11' as flag from mytable union select col12 as col_value,'12' as flag from mytable ) a where flag<=to_char(sysdate,'MM');
select sum(col_value) from
(
select 1 col_month, col_1 col_value, col_others from tab_name
union all
select 2 col_month, col_2 col_value, col_others from tab_name
union all
select 3 col_month, col_3 col_value, col_others from tab_name
union all
select 4 col_month, col_3 col_value, col_others from tab_name
union all
select 5 col_month, col_5 col_value, col_others from tab_name
union all
select 6 col_month, col_6 col_value, col_others from tab_name
union all
select 7 col_month, col_7 col_value, col_others from tab_name
union all
select 8 col_month, col_8 col_value, col_others from tab_name
union all
select 9 col_month, col_9 col_value, col_others from tab_name
union all
select 10 col_month, col_10 col_value, col_others from tab_name
union all
select 11 col_month, col_11 col_value, col_others from tab_name
union all
select 12 col_month, col_12 col_value, col_others from tab_name
)
where ...... and col_month <= to_number(to_char(sysdate, 'mm')) ;
from (
select col1 as col_value,'1' as flag from mytable
union
select col2 as col_value,'2' as flag from mytable
union
select col3 as col_value,'3' as flag from mytable
union
select col4 as col_value,'4' as flag from mytableunion
select col5 as col_value,'5' as flag from mytable
union
select col6 as col_value,'6' as flag from mytable
union
select col7 as col_value,'7' as flag from mytable
union
select col8 as col_value,'8' as flag from mytableunion
select col9 as col_value,'9' as flag from mytable
union
select col10 as col_value,'10' as flag from mytable
union
select col11 as col_value,'11' as flag from mytable
union
select col12 as col_value,'12' as flag from mytable
) a
where flag<=to_char(sysdate,'MM');