How to reuse a result column in an expression for another result column

前端 未结 9 2284
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 10:16

Example:

SELECT
   (SELECT SUM(...) FROM ...) as turnover,
   (SELECT SUM(...) FROM ...) as cost,
   turnover - cost as profit

Sure this is

相关标签:
9条回答
  • 2020-12-25 10:20

    You could reuse the query like this:

    WITH 
      TURNOVER AS (
        SELECT SUM(...) FROM ...)
      ),
      COST AS(
        SELECT SUM(...) FROM ...
      )
    
    SELECT *
    FROM(
     SELECT
       TURNOVER.sum as SUM_TURNOVER
     FROM
     TURNOVER,COST
     WHERE ....
    ) AS a
    

    This is equivalent to :

    SELECT *
    FROM(
     SELECT
       TURNOVER.sum as SUM_TURNOVER
     FROM
     (
       SELECT SUM(...) FROM ...)
     )AS TURNOVER,
     (
       SELECT SUM(...) FROM ...
     )AS COST
     WHERE ....
    ) AS a
    

    There is a point to note here. The first method is more readable and reusable, but the second method might be faster, because the DB might choose a better plan for it.

    0 讨论(0)
  • 2020-12-25 10:23

    this is pretty old but i ran into this problem and saw this post but didnt manage to solve my problem using the given answers so i eventually arrived at this solution :

    if your query is :

    SELECT
       (SELECT SUM(...) FROM ...) as turnover,
       (SELECT SUM(...) FROM ...) as cost,
       turnover - cost as profit
    

    you can turn it into a subquery and then use the fields such as :

    SELECT *,(myFields.turnover-myFields.cost) as profit 
    FROM
    (      
    SELECT
           (SELECT SUM(...) FROM ...) as turnover,
           (SELECT SUM(...) FROM ...) as cost
    
    ) as myFields
    

    i'm not entirely sure if this is a bad way of doing things but performance wise it seems okay for me querying over 224,000 records took 1.5 sec not sure if its later on turned into 2x of the same sub query by DB.

    0 讨论(0)
  • 2020-12-25 10:26

    Use a cross apply or outer apply.

    SELECT
      Calc1.turnover,
      Calc2.cost,
      Calc3.profit
    from
       cross apply ((SELECT SUM(...) as turnover FROM ...)) as Calc1
       cross apply ((SELECT SUM(...) as cost FROM ...)) as Calc2
    
       /*
         Note there is no from Clause in Calc 3 below.
         This is how you can "stack" formulas like in excel.
         You can return any number of columns, not just one.
       */
       cross apply (select Calc1.turnover - Calc2.cost as profit) as Calc3
    
    0 讨论(0)
  • 2020-12-25 10:27

    You can use user defined variables like this

    SELECT
       @turnover := (SELECT SUM(...) FROM ...),
       @cost := (SELECT SUM(...) FROM ...),
       @turnover - @cost as profit
    

    http://dev.mysql.com/doc/refman/5.7/en/user-variables.html

    0 讨论(0)
  • 2020-12-25 10:28

    Like so:

    SELECT
       turnover,
       cost,
       turnover - cost as profit
    from (
       (SELECT SUM(...) FROM ...) as turnover,
       (SELECT SUM(...) FROM ...) as cost
       ) as partial_sums
    
    0 讨论(0)
  • 2020-12-25 10:32

    I think the following will work:

    SELECT turnover, cost, turnover-cost as profit FROM
       (SELECT 1 AS FAKE_KEY, SUM(a_field) AS TURNOVER FROM some_table) a
    INNER JOIN
       (SELECT 1 AS FAKE_KEY, SUM(a_nother_field) AS COST FROM some_other_table) b
    USING (FAKE_KEY);
    

    Not tested on animals - you'll be first! :-)

    Share and enjoy.

    0 讨论(0)
提交回复
热议问题