Sorted difference between two columns

前端 未结 2 351
暖寄归人
暖寄归人 2021-01-14 03:59

I have two columns (buying prince and sale price) and I want to calculate the difference between them. After that I want to order the result so I can see all the profit marg

相关标签:
2条回答
  • 2021-01-14 04:53
      SELECT (sale_price - buy_price) AS profit
        FROM table_name
    ORDER BY profit DESC
    
    0 讨论(0)
  • 2021-01-14 05:01

    Joe has it, but I think you might be looking for something slightly different for the ordering.

    Profit margin is defined as net income / revenue.. so the profit margin of each product would be (sale_price minus buy price) divided by sale_price.

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

    Good luck.

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