mysql - Arithmetic operations between SQL select results -
hi need part of bigger script:
select group_concat(distinct concat('(sum(case when columna = "' ,columna, ' "then 1 else 0 end))/(select total hd_totals columna = "' ,columna, ' ") "' ,columna, ' "')) table inner join.....
the problem can't / operation because gives error, if select output single value. don't know how use aliases or store in variable because it's rather complex (to me) environment i'm creating here...
what : use cast have decimal type, , use coalesce prevent null values?
select group_concat(distinct concat('coalesce(cast((sum(case when columna = "' ,columna, ' "then 1 else 0 end)) decimal(10,4)),0)/coalesce(cast((select total hd_totals columna = "' ,columna, ' ") decimal(10,4)),0.01) "' ,columna, ' "')) table inner join.....
note : there arbitrary constant 0.01 prevent dividing 0.
more important note : if add case when check don't divide 0.
Comments
Post a Comment