MySQL:使用两列的值来计算第三列

时间:2013-10-29 14:27:47

标签: mysql sql

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

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 Revenue 查询计算,这似乎是一种愚蠢的做法。但显然我不能只说"Total Revenue" - "Total Costs" AS "Margin

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

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

正如@ X.L.Ant所提议的,您可以包装查询并在包装器查询中执行简单的数学运算。但是,在您的方案中,如果您只是简化查询,则可以更有效地执行此操作:

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