1

我正在尝试运行一份报告,该报告将为我提供过去 3 个月的每日“情节”销售额,但我很难让 SUM() 正确加起来。单个 Transaction_ID 可能跨越多个 Plot 记录。通过下面的查询,如果有 2 个具有相同 Transaction_ID 的地块,则将它们加在一起,这不是我想要的。我需要它在 SUM() 中只使用一次事务行。

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                   |                | 
+------------------------+-------------+------+-----+---------------------+----------------+
4

2 回答 2

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; 
于 2012-05-21T20:51:21.920 回答
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

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

于 2012-05-21T11:59:02.833 回答