Using CASE and MERGE in SQL