How to use SUM() in MySQL for calculated values -


in mysql, dont know how use function sum calculated values.

could me please? in code below, latest part. thing want add row @ end, summarizing "calculado" column

thank in advance

select  p.id_order ref, concat (di.firstname,' ',di.lastname) nombre, ca.weight peso,   if (ca.weight<1,(1),     if (ca.weight>=1 , (ca.weight<3),(2),         if (ca.weight>=3 , (ca.weight<5),(3),             if (ca.weight>=5 , (ca.weight<8),(4),                 if (ca.weight>=8 , (ca.weight<10),(5),                     if (ca.weight>=10 , (ca.weight<15),(6),                         if (ca.weight>=15 , (ca.weight<20),(7),                             if (ca.weight>=20,(ca.weight*(0.5)),'error'   )))))))) calculado,   p.invoice_date fecha  ps_orders p left join ps_address di on (p.id_address_delivery = di.id_address) left join ps_order_carrier ca on (p.id_order = ca.id_order)  date(p.invoice_date) >= concat(date_format(last_day(now() - interval 1 month),'%y-%m-'),'01')  ,  date(p.invoice_date) <= concat(date_format(last_day(now() - interval 1 month),'%y-%m-'),'31')   group p.id_order  union  select '', '',sum (calculado),'','' 

select  p.id_order ref, concat (di.firstname,' ',di.lastname) nombre, sum(ca.weight) peso,  sum( if (ca.weight<1,(1),     if (ca.weight>=1 , (ca.weight<3),(2),         if (ca.weight>=3 , (ca.weight<5),(3),             if (ca.weight>=5 , (ca.weight<8),(4),                 if (ca.weight>=8 , (ca.weight<10),(5),                     if (ca.weight>=10 , (ca.weight<15),(6),                         if (ca.weight>=15 , (ca.weight<20),(7),                             if (ca.weight>=20,(ca.weight*(0.5)),'error'   )))))))) ) calculado,   p.invoice_date fecha  ps_orders p left join ps_address di on (p.id_address_delivery = di.id_address) left join ps_order_carrier ca on (p.id_order = ca.id_order)  date(p.invoice_date) >= concat(date_format(last_day(now() - interval 1 month),'%y-%m-'),'01')  ,  date(p.invoice_date) <= concat(date_format(last_day(now() - interval 1 month),'%y-%m-'),'31')   group p.id_order rollup 

with rollup clause add result summary row. function sum() need correct work group by.

by way, offer replace mad "if" via table:

create table weight_calc(wfrom int, wto int, result decimal(10,1)); insert weight_calc values(-10000,1,1), (1,3,2), (3,5,3), (5,8,4),                               (8,10,5), (10,15,6), (15,20,7);  replace in query:  .... sum( coalesce(w.result,ca.weight*0.5) ) calculado .... ps_order_carrier ca on (p.id_order = ca.id_order) left join weight_calc w on w.wto>=ca.weight , w.wfrom<ca.weight 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -