--
-- ### Advanced case cond
--

drop if exists table t1;
create table t1 ( a int, v1 decimal(6), v2 decimal(6), v3 decimal(6));
 
insert into t1 values ( 1, 120, 2990, 30);
insert into t1 values ( 1, 145, 2450, 30);
insert into t1 values ( 2, 143, 23, 30);
insert into t1 values ( 2, 877, 245, 30);
insert into t1 values ( 2, 1, 130, 30);
insert into t1 values ( 3, 10, 20, 30);
 
drop if exists view v1;
create view v1 as select
        a as a,
        sum(v1) as sv1,
        sum(v2) as sv2,
        sum(v3) as sv3
from t1 group by a;
 
drop if exists view v2;
create view v2 as select
 
        a as a,
        case when sv1 is not null and sv2 is not null
                then 100 * sv1 / sv2
        else (decimal)42.000000 end as svpart -- fixed dimension must match
from v1;

desc view v2;

select * from v2;
