这是一个可怕的查询,但是对您已有的内容进行简单的扩展可以满足我认为您所要求的:
SELECT t1.order_id,
t3.price * SUM( t1.quantity ) AS subtotal,
( SUM( t1.discount ) + t3.discount ) / 100 AS disc,
t3.price * SUM( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 AS tax,
t3.price * SUM( t1.quantity ) - ( SUM( t1.discount ) + t3.discount ) / 100 + t3.price * SUM( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 AS total,
CASE t2.payment_type WHEN 'Cash'
THEN 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 AS cash,
CASE t2.payment_type WHEN 'Card'
THEN 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 AS card,
CASE t2.payment_type WHEN 'Check'
THEN 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 AS check,
CASE WHEN t2.payment_type IN ('Cash', 'Card', 'Check')
THEN 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 AS cash_card_check
FROM pos_item_order AS t1
JOIN pos_order AS t2 ON t2.order_id = t1.order_id
JOIN inv_item AS t3 ON t3.item_id = t1.item_id
GROUP BY t1.order_id, t1.item_i
The IN(...)
符号可能不起作用;如果没有,你可以写一个三路OR
条件代替。但是,我忍不住认为必须有更好的方法来构建查询。
此查询为您提供基本详细信息,包括付款类型。将其保存到临时表中,或者在WITH子句中使用命名子表达式(如果您的DBMS支持的话)。
SELECT t1.order_id,
t2.payment_type,
t3.price * SUM( t1.quantity ) AS subtotal,
( SUM( t1.discount ) + t3.discount ) / 100 AS disc,
t3.price * SUM( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 AS tax,
t3.price * SUM( t1.quantity ) - ( SUM( t1.discount ) + t3.discount ) / 100 + t3.price * SUM( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 AS total,
t3.price * SUM( t1.quantity ) - ( SUM( t1.discount ) + t3.discount ) / 100 + t3.price * SUM( t1.quantity ) * ( t3.tax_state + t3.tax_fed ) /100 AS payment
FROM pos_item_order AS t1
JOIN pos_order AS t2 ON t2.order_id = t1.order_id
JOIN inv_item AS t3 ON t3.item_id = t1.item_id
GROUP BY t1.order_id, t1.item_i, t2.payment_type
然后,您可以分别或合并汇总卡、支票和现金。