Skip to content
Advertisement

How to use IF statement in a calculation in a SQL query

I use following query to give me the earned commission of our sales agents.

Now I want the output to be sorted by (sales * sph * factor). Where factor is 10 if sph > 1.5 and else 7.5.

Is it possible to implement this in the query. If yes how do I achieve this?

It would also be good if calculation would be done in the SELECT statement like (sales * sph * factor) as commission and then just use SORT BY commission.

I hope my question makes any sense.

Advertisement

Answer

You could use condition in order by eg using a case when

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement