Sorted difference between two columns

心不动则不痛 提交于 2019-12-01 07:05:56

问题


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 margins.

Can I do it with just one SELECT statement?

Thanks!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/7379423/sorted-difference-between-two-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!