【问题标题】:Sorted difference between two columns两列之间的排序差异
【发布时间】:2013-09-02 20:44:24
【问题描述】:

我有两列(购买价格和销售价格),我想计算它们之间的差异。之后我想订购结果,这样我就可以看到所有的利润率。

我可以只用一个 SELECT 语句吗?

谢谢!

【问题讨论】:

    标签: mysql sql-order-by


    【解决方案1】:
      SELECT (sale_price - buy_price) AS profit
        FROM table_name
    ORDER BY profit DESC
    

    【讨论】:

      【解决方案2】:

      Joe 有,但我认为您可能正在寻找与订购略有不同的东西。

      利润率定义为净收入/收入。因此每种产品的利润率将是(销售价格减去购买价格)除以销售价格。

      SELECT (sale_price - buy_price) AS profit FROM table_name 
      ORDER BY ((sale_price - buy_price) / sale_price) DESC
      

      祝你好运。

      【讨论】:

      • 为什么不在ORDER BY 中重复使用profit 而不是再次使用sale_price - buy_price? :)
      • @Joe True。没想到那么远:)
      猜你喜欢
      • 1970-01-01
      • 2016-06-03
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多