如何为SUM()选择DISTINCT行?

时间:2012-05-21 11:48:33

标签: mysql

我正在尝试运行一份报告,该报告会在过去3个月内每天给出“情节”销售情况,而且我很难让SUM()正确累加。单个Transaction_ID可以跨越多个Plot记录。通过下面的查询,如果有2个具有相同Transaction_ID的图,则将它们一起添加,这不是我想要的。我需要它只在SUM()中使用一次Transaction行。

SELECT 
    DATE_FORMAT(Transactions.CreateDate,'%m-%d') AS MonthAndDay, 
    DATE_FORMAT(Transactions.CreateDate,'%M, %D') AS Day, 
    SUM(Transactions.AmountTotal) AS DailySales 
FROM 
    Transactions, 
    Plots 
WHERE 
    Transactions.Transaction_ID = Plots.Transaction_ID AND 
    Transactions.CreateDate <= CURDATE() AND 
    Transactions.CreateDate >= DATE_SUB(CURDATE(),INTERVAL 3 MONTH) 
GROUP BY 
    MonthAndDay 
ORDER BY 
    MonthAndDay ASC 

表结构如下:

+-----------------------------------+----------------------------------------------------------------------------------------------------+------+-----+---------------------+----------------+
| Field                             | Type                                                                                               | Null | Key | Default             | Extra          |
+-----------------------------------+----------------------------------------------------------------------------------------------------+------+-----+---------------------+----------------+
| Transaction_ID                    | int(11)                                                                                            | NO   | PRI | NULL                | auto_increment | 
| CreateDate                        | datetime                                                                                           | NO   |     | 0000-00-00 00:00:00 |                | 
| AmountTotal                       | decimal(10,2)                                                                                      | NO   |     | 0.00                |                | 
+-----------------------------------+----------------------------------------------------------------------------------------------------+------+-----+---------------------+----------------+

+------------------------+-------------+------+-----+---------------------+----------------+
| Field                  | Type        | Null | Key | Default             | Extra          |
+------------------------+-------------+------+-----+---------------------+----------------+
| Plot_ID                | int(11)     | NO   | PRI | NULL                | auto_increment | 
| Transaction_ID         | int(11)     | YES  | MUL | NULL                |                | 
| Plot                   | varchar(10) | NO   |     | 0                   |                | 
+------------------------+-------------+------+-----+---------------------+----------------+

2 个答案:

答案 0 :(得分:1)

以下解决方案有效,尽管有子查询:

SELECT
   DATE_FORMAT(Transactions.CreateDate,'%m-%d') AS MonthAndDay,
   DATE_FORMAT(Transactions.CreateDate,'%M, %D') AS Day,
   SUM(Transactions.AmountTotal) AS DailySales
FROM
   Transactions
WHERE
   Transactions.Transaction_ID IN (SELECT DISTINCT Transaction_ID FROM Plots) AND
   Transactions.CreateDate <= CURDATE() AND
   Transactions.CreateDate >= DATE_SUB(CURDATE(),INTERVAL 3 MONTH)
GROUP BY
   MonthAndDay
ORDER BY
   MonthAndDay ASC; 

答案 1 :(得分:0)

脱离我的头脑,我认为以下方法可行,但我没有测试过,所以可能是错的:

SELECT 
    DISTINCT Transactions.Transaction_ID,
    DATE_FORMAT(Transactions.CreateDate,'%m-%d') AS MonthAndDay, 
    DATE_FORMAT(Transactions.CreateDate,'%M, %D') AS Day, 
    SUM(Transactions.AmountTotal) AS DailySales 
FROM 
    Transactions, 
    Plots 
WHERE 
    Transactions.Transaction_ID = Plots.Transaction_ID AND 
    Transactions.CreateDate <= CURDATE() AND 
    Transactions.CreateDate >= DATE_SUB(CURDATE(),INTERVAL 3 MONTH) 
GROUP BY 
    MonthAndDay 
ORDER BY 
    MonthAndDay ASC 

第二次去:

SELECT 
    DATE_FORMAT(Transactions.CreateDate,'%m-%d') AS MonthAndDay, 
    DATE_FORMAT(Transactions.CreateDate,'%M, %D') AS DAY, 
    SUM(Transactions.AmountTotal) AS DailySales,
    COUNT(Transactions.Transaction_ID) AS Tmp
FROM 
    Transactions, 
    Plots
WHERE 
    Transactions.Transaction_ID = Plots.Transaction_ID AND 
    Transactions.CreateDate <= CURDATE() AND 
    Transactions.CreateDate >= DATE_SUB(CURDATE(),INTERVAL 3 MONTH) 
GROUP BY 
    Plots.Plot_ID, MonthAndDay 
ORDER BY 
    MonthAndDay ASC

这在Plots表中产生了3行,有两行引用Transaction_ID 2,一行引用Transaction_ID 1

这导致出现三个绘图行,但总和值显示正确。