Mysql 如何获取列的总和
我有下面的查询,它提供了我想要的数据,但是,我需要我的案例报表中的现金、信用和支票列的总和。我怎样才能做到这一点?如果可能的话,我想使用一个程序 而且,对我来说,这个查询似乎没有那么有效。有人能改进这一点吗?在我看来,我应该能够设置变量,然后在我的CASE语句中使用它们,但不确定如何完成Mysql 如何获取列的总和,mysql,Mysql,我有下面的查询,它提供了我想要的数据,但是,我需要我的案例报表中的现金、信用和支票列的总和。我怎样才能做到这一点?如果可能的话,我想使用一个程序 而且,对我来说,这个查询似乎没有那么有效。有人能改进这一点吗?在我看来,我应该能够设置变量,然后在我的CASE语句中使用它们,但不确定如何完成 SELECT t1.order_id, t3.price * SUM( t1.quantity ) AS subtotal, ( SUM( t1.discount ) + t3.discount ) / 100
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
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_id
编辑:当我说“需要现金、信用和支票列的总和”时,我指的是每列的各自总数。不幸的是,您不能在另一个表达式中使用派生列,但您可以实现您想要的结果
过了我的就寝时间,如果明天有人还没有超过我,我就去试试,但我想我会在这段时间给你指明方向。这是一个可怕的问题,但简单地扩展一下你已经拥有的,我想你会得到你想要的:
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
(…)符号中的可能不起作用;如果没有,您可以编写一个三向或
条件。然而,我不得不认为必须有一种更好的方法来构造您的查询
此查询提供基本详细信息,包括付款类型。将其保存到临时表中,或者如果DBMS支持,则在WITH子句中使用命名子表达式
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
然后,您可以将卡、支票和现金分别合并。是否使用汇总功能执行所需操作。。我以前没用过。我现在就读一点。谢谢你的建议,Martin。@Martin,看来加上rollup
会水平合计行数,而我需要垂直合计。谢谢你在这方面的努力。你是对的,我的问题实际上看起来很可怕,我同意,一定有更好的方法来组织它。我运行了您的查询,它运行得很好,但是,您似乎正在合计各个行。我要找的是支票、信用卡和现金栏的总和。这就是你想要的吗?@Jim:第二个查询是中间结果;然后,您将重新处理它以计算现金、支票、卡和总计。至少,这是我的理论。我不太清楚单笔订单是否部分用现金支付,部分用支票支付,部分用信用卡支付。您没有为我们提供足够的架构信息来确定。我倾向于假设一个订单将有一个单一的付款方式,但我可能错了。啊。。。我知道你现在在做什么。谢谢你的努力。乔纳森,如果你必须重写这个查询,你会怎么做?