mysql - How to get the total sum for a column -


i have following query gives me data want, however, need total sum of cash, credit , check columns inside case statements. how can achieve this? i'd use procedure if possible.

also, me, query doesn't seem @ efficient. can improve upon this? seems me should able set variables , use them inside case statements not sure how it's done.

select t1.order_id, t3.price * sum( t1.quantity ) subtotal, ( sum( t1.discount ) + t3.discount ) / 100 disc, t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 tax, t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 total, case t2.payment_type when 'cash'  t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 else 0.00 end cash, case t2.payment_type when 'card'  t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 else 0.00 end card, case t2.payment_type when 'check' t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 else 0.00 end check pos_item_order t1 join pos_order t2 on t2.order_id = t1.order_id join inv_item t3 on t3.item_id = t1.item_id group t1.order_id, t1.item_id 

edit: when "need total sum of cash, credit , check columns", mean respective totals per column.

that gruesome query, simple-minded extension have gives think asking for:

select t1.order_id,        t3.price * sum( t1.quantity ) subtotal,        ( sum( t1.discount ) + t3.discount ) / 100 disc,        t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 tax,        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 total,        case t2.payment_type when 'cash'        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100        else 0.00 end cash,        case t2.payment_type when 'card'        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100        else 0.00 end card,        case t2.payment_type when 'check'        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100        else 0.00 end check,        case when t2.payment_type in ('cash', 'card', 'check')        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 else 0.00 end cash_card_check   pos_item_order t1   join pos_order t2 on t2.order_id = t1.order_id   join inv_item t3 on t3.item_id = t1.item_id  group t1.order_id, t1.item_i 

the in(...) notation might not work; if not, can write out three-way or condition instead. however, can't think there has better way of structuring query.

this query gives basic details, including payment type. save temporary table, or use named subexpression in clause if dbms supports that.

select t1.order_id,        t2.payment_type,        t3.price * sum( t1.quantity ) subtotal,        ( sum( t1.discount ) + t3.discount ) / 100 disc,        t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 tax,        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 total,        t3.price * sum( t1.quantity ) - ( sum( t1.discount ) + t3.discount ) / 100 + t3.price * sum( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 payment   pos_item_order t1   join pos_order      t2 on t2.order_id = t1.order_id   join inv_item       t3 on t3.item_id = t1.item_id  group t1.order_id, t1.item_i, t2.payment_type 

you can aggregate cards, checks , cash separately , jointly.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -