Here is another fancy way of dealing with NULLs if you have some equations you have to run.
Let's say that you work for an organization that deals with different salary types:
- Hourly
- Salary
- Commissions
SQL COALESCE
COALECE allows you to specify a comma delimited list of expressions and it will return the first non-NULL expression. So, in our example above, we have columns that show an employee's: hourly wage, salary, commission amount and number of sales. We can then specify each expression with the wage calculation, as seen below:
SELECT COALESCE(hourly_wage * 40 * 52, salary, commission * salesnum) AS 'Total Salary'
FROM wages
This will then return the wage for each employee, no matter if they are hourly, salary or commission based.
No comments:
Post a Comment