MySQL - 基于同一表中的行求和列值
Posted
技术标签:
【中文标题】MySQL - 基于同一表中的行求和列值【英文标题】:MySQL - sum column value(s) based on row from the same table 【发布时间】:2013-01-26 04:24:08 【问题描述】:我正在尝试根据同一张表中的 ProductID 在新列中获取“现金”、“支票”和“信用卡”总计。
表格 - 付款
+-----------+------------+---------------+--------+
| ProductID | SaleDate | PaymentMethod | Amount |
+-----------+------------+---------------+--------+
| 3 | 2012-02-10 | Cash | 10 |
| 3 | 2012-02-10 | Cash | 10 |
| 3 | 2012-02-10 | Check | 15 |
| 3 | 2012-02-10 | Credit Card | 25 |
| 4 | 2012-02-10 | Cash | 5 |
| 4 | 2012-02-10 | Check | 6 |
| 4 | 2012-02-10 | Credit Card | 7 |
+-----------+------------+---------------+--------+
期望的输出 -
+------------+------+-------+-------------+-------+
| ProductID | Cash | Check | Credit Card | Total |
+------------+------+-------+-------------+-------+
| 3 | 20 | 15 | 25 | 60 |
| 4 | 5 | 6 | 7 | 18 |
+------------+------+-------+-------------+-------+
我已经尝试 LEFT JOINing 同一张表,但没有任何成功。任何建议,将不胜感激。谢谢。
不成功且不完整的尝试 -
SELECT P.ProductID, Sum( PCash.Amount ) AS 'Cash', SUM( PCheck.Amount ) AS 'Check', SUM( PCredit.Amount) AS 'Credit Card'
FROM Payments AS P
LEFT JOIN Payments AS PCash ON P.ProductID = PCash.ProductID AND PCash.PaymentMethod = 'Cash'
LEFT JOIN Payments AS PCheck ON P.ProductID = PCheck.ProductID AND PCheck.PaymentMethod = 'Check'
LEFT JOIN Payments AS PCredit ON P.ProductID = PCredit.ProductID AND PCredit.PaymentMethod = 'Credit'
WHERE P.SaleDate = '2012-02-10' GROUP BY ProductID;
【问题讨论】:
你是不是用工具生成了-
和+
s组成的表格UI?
【参考方案1】:
我认为你让这变得比它需要的复杂一点。
SELECT
ProductID,
SUM(IF(PaymentMethod = 'Cash', Amount, 0)) AS 'Cash',
-- snip
SUM(Amount) AS Total
FROM
Payments
WHERE
SaleDate = '2012-02-10'
GROUP BY
ProductID
【讨论】:
@diEcho 我没有输入那部分,因为我认为他可以毫无困难地自己解决这个问题 我想要相同的SaleDate
和相同的PaymentMethod
金额总和,否则继续。
Kudos @Explosion Pills,你拯救了我的一天,非常感谢的回答,但不能提供足够的,但 +1【参考方案2】:
这可能看起来有点复杂,但完全符合您的要求
SELECT
DISTINCT(p.`ProductID`) AS ProductID,
SUM(pl.CashAmount) AS Cash,
SUM(pr.CashAmount) AS `Check`,
SUM(px.CashAmount) AS `Credit Card`,
SUM(pl.CashAmount) + SUM(pr.CashAmount) +SUM(px.CashAmount) AS Amount
FROM
`payments` AS p
LEFT JOIN (SELECT ProductID,PaymentMethod , IFNULL(Amount,0) AS CashAmount FROM payments WHERE PaymentMethod = 'Cash' GROUP BY ProductID , PaymentMethod ) AS pl
ON pl.`PaymentMethod` = p.`PaymentMethod` AND pl.ProductID = p.`ProductID`
LEFT JOIN (SELECT ProductID,PaymentMethod , IFNULL(Amount,0) AS CashAmount FROM payments WHERE PaymentMethod = 'Check' GROUP BY ProductID , PaymentMethod) AS pr
ON pr.`PaymentMethod` = p.`PaymentMethod` AND pr.ProductID = p.`ProductID`
LEFT JOIN (SELECT ProductID, PaymentMethod , IFNULL(Amount,0) AS CashAmount FROM payments WHERE PaymentMethod = 'Credit Card' GROUP BY ProductID , PaymentMethod) AS px
ON px.`PaymentMethod` = p.`PaymentMethod` AND px.ProductID = p.`ProductID`
GROUP BY p.`ProductID` ;
输出
ProductID | Cash | Check | Credit Card | Amount
-----------------------------------------------
3 | 20 | 15 | 25 | 60
4 | 5 | 6 | 7 | 18
SQL Fiddle Demo
【讨论】:
【参考方案3】:SUM CASE 使用示例:
SELECT
DISTINCT(p.`ProductID`) AS ProductID,
SUM(IF(p.`PaymentMethod`='Cash',Amount,0)) AS Cash_,
SUM(IF(p.`PaymentMethod`='Check',Amount,0)) AS Check_,
SUM(IF(p.`PaymentMethod`='Credit Card',Amount,0)) AS Credit_Card_,
SUM( CASE PaymentMethod
WHEN 'Cash' THEN Amount
WHEN 'Check' THEN Amount
WHEN 'Credit Card' THEN Amount
END) AS Total
FROM
`payments` AS p
GROUP BY p.`ProductID`;
SQL 小提琴: http://www.sqlfiddle.com/#!9/23d07d/18
【讨论】:
以上是关于MySQL - 基于同一表中的行求和列值的主要内容,如果未能解决你的问题,请参考以下文章