mysql - 如何获取列的总和

标签 mysql

我有以下查询可以提供我想要的数据,但是,我需要 CASE 语句中的现金、信用和支票列的总和。我怎样才能做到这一点?如果可能的话,我想为此使用一个程序。

另外,对我来说,这个查询似乎根本没有那么有效。任何人都可以对此进行改进吗?在我看来,我应该能够设置变量,然后在我的 CASE 语句中使用它们,但不确定它是如何完成的。

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

IN(...) 符号可能不起作用;如果没有,您可以写出一个三向 OR 条件。但是,我不禁认为必须有更好的方法来构建您的查询。

此查询为您提供基本详细信息,包括付款类型。将其保存到临时表中,或者如果您的 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

然后,您可以分别或联合汇总卡、支票和现金。

关于mysql - 如何获取列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802244/

相关文章:

mysql - 'column-adding'(模式修改)是 NoSQL(mongodb)数据库相对于 MySQL 等 RDBMS 的关键优势吗

MYSQL/PHP JOIN 和排除条件

mysql - 在 MySQL 数据库中标记大量随机记录 - 仅一次

php - 如何修复 PDO 中无效的参数号

mysql - 查询时使用 NOT IN 和 NOT EQUAL

mysql - 我应该在 google cloud run 上运行 mysql 吗? (或任何数据库)

php - 如何使用php和mysqli一次查询获取多个表的数据?

带有日期函数查询的mysql运行缓慢

php - 适用于大型和可扩展应用程序的数据库表结构

用于构建动态填充模型的 mySQL 查询