V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
SummerWQM
V2EX  ›  问与答

我这条 SQL 计算了多次 sum 我想在第一次 sum 后就只使用就好了不每次 计算 咋个写好点儿

  •  
  •   SummerWQM · Sep 20, 2017 · 2420 views
    This topic created in 3141 days ago, the information mentioned may be changed or developed.

    select *,sum(product_retail) revenue,sum(product_cost) gross_profit,

                    round((sum(`product_retail`)-sum(`product_cost`))/sum(`product_retail`) * 100, 2) gross_profit_percent
    
               from `combine_product` cp
    
               left join `order_product` op on cp.product_id = op.product_id
    
               where cp.is_parent = 1 group by op.product_id
    
    2 replies    2017-09-21 09:27:45 +08:00
    F281M6Dh8DXpD1g2
        1
    F281M6Dh8DXpD1g2  
       Sep 20, 2017
    select
    your-select-list
    from
    (
    select
    sum(product_retail) revenue,sum(product_cost) gross_profit
    from `combine_product` cp

    left join `order_product` op on cp.product_id = op.product_id

    where cp.is_parent = 1 group by op.product_id
    ) dt
    SummerWQM
        2
    SummerWQM  
    OP
       Sep 21, 2017
    明白 思路 嵌套一个
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:02 · PVG 15:02 · LAX 00:02 · JFK 03:02
    ♥ Do have faith in what you're doing.