0

我确定这是一个常见问题,但我无法按照我所描述的方式找到解决方案。我的查询是这样的:

SELECT 
    (
        SELECT SUM(finance_transaction_components.invoiced_price) 
        FROM finance_transactions
        JOIN finance_transaction_components ON finance_transaction_components.transaction_id = finance_transactions.id
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE DATE_FORMAT(finance_transactions.completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29"
    ) AS "Total Costs",

    (
        SELECT SUM(finance_transactions.agent_price)
        FROM finance_transactions
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE finance_transactions.product_id != 10
        AND DATE_FORMAT(completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29" 
    ) AS "Total Revenue",

    (
        SELECT SUM(finance_transactions.agent_price)
        FROM finance_transactions
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE finance_transactions.product_id != 10
        AND DATE_FORMAT(completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29" 
        AND IF(finance_transactions.lldrg > 0, IF(finance_transactions.lldrg = 1, 0, 1) ,1) = 1
    ) - ( SELECT SUM(finance_transaction_components.invoiced_price) 
        FROM finance_transactions
        JOIN finance_transaction_components ON finance_transaction_components.transaction_id = finance_transactions.id
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE DATE_FORMAT(finance_transactions.completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29"
    )   AS "Margin"

FROM finance_transactions
GROUP BY finance_transactions.agent_id

在这种情况下,列是什么并不重要,重要的是:Margin需要重复Total CostsTotal Revenue查询来计算,这似乎是一种愚蠢的做法。但显然我不能只说"Total Revenue" - "Total Costs" AS "Margin

4

2 回答 2

1

只需使用内部查询,您就可以在不重复代码的情况下实现相同的目标:

SELECT TotalCosts, TotalRevenue, TotalCost - TotalRevenue AS Margin
FROM 
(
    SELECT 
    (
        SELECT SUM(finance_transaction_components.invoiced_price) 
        FROM finance_transactions
        JOIN finance_transaction_components ON finance_transaction_components.transaction_id = finance_transactions.id
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE DATE_FORMAT(finance_transactions.completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29"
    ) AS TotalCosts,

    (
        SELECT SUM(finance_transactions.agent_price)
        FROM finance_transactions
        JOIN agents ON agents.id = finance_transactions.agent_id
        WHERE finance_transactions.product_id != 10
        AND DATE_FORMAT(completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29" 
    ) AS TotalRevenue
) t

编辑:此答案基于问题的第一个版本,即具有聚合函数的版本。

于 2013-10-29T14:34:03.333 回答
1

更新 - 此答案基于原始问题的查询。

正如@XLant 所建议的那样,您可以包装查询并在包装查询中进行简单的数学运算。但是,在您的场景中,如果您将查询简化为以下内容,则可以更有效地执行此操作:

SELECT agent_id, TotalCost, TotalRevenue, TotalRevenue - TotalCost AS Margin
FROM (
    SELECT finance_transactions.agent_id
           SUM(finance_transaction_totals.total_cost) AS TotalCost,
           SUM(CASE WHEN finance_transactions.product_id = 10 THEN 0 
                    ELSE finance_transactions.agent_price) AS TotalRevenue 
    FROM finance_transactions
    JOIN (SELECT transaction_id, SUM(invoiced_price) AS total_cost
          FROM finance_transaction_components
          GROUP BY transaction_id) finance_transaction_totals
       ON finance_transactions.id = finance_transaction_totals.transaction_id
    WHERE DATE_FORMAT(finance_transactions.completed_date, "%Y-%m-%d") BETWEEN "2013-10-01" AND "2013-10-29"
    GROUP BY finance_transactions.agent_id 
) t
于 2013-10-29T14:45:58.630 回答