I have a table as below:
Job Quantity Status
-----------------------
1 100 OK
2 400 HOLD
3 200 HOLD
4 450 OK
5 300
6 500
I would like my result to be shown as below:
Job Quantity Status
----------------------
4 450 OK
2 400 HOLD
1 100 OK
3 200 HOLD
6 500
5 300
I have created this query but it’s not working when the table contains data where the status column is null/empty
SELECT
Job,
Quantity,
Status
FROM
myTable
ORDER BY CASE
WHEN QUANTITY >= 400 AND STATUS = 'OK' THEN 1
WHEN QUANTITY >= 400 AND STATUS = 'HOLD' THEN 2
WHEN QUANTITY < 400 AND STATUS = 'OK' THEN 3
WHEN QUANTITY >= 400 AND STATUS = 'HOLD' THEN 4
WHEN QUANTITY >= 400 AND STATUS = '' THEN 5
WHEN QUANTITY < 400 AND STATUS = '' THEN 6
END
Unless I’m misreading, I feel like you’ve answered your own question. You need to account for when your data is
NULL.The important part is that
NULLis of a different type than''. Just like'' = 0is false, so isNULL = ''.NULLis used to represent vacuous values. Think about it in terms of boolean values instead of string. Obviously there are times where something is neither true nor false, this is where a value likeNULLwould come in. Similarly, if you think of strings as pure data instead of characters and words then there is a difference between the empty value and no value at all.For more information, see http://www.w3schools.com/sql/sql_null_values.asp
So I think your code should look like:
Unless you explictly set the STATUS to
'', you might be able to just useSTATUS IS NULL.