问题
I need to create a table with a user id and an assigned value. I have these three select statements:
select sales_person_id from promotions where
sales > 30000 and city = ‘Georgia’
select sales_person_id from promotions
where sales > 50000 and city = ‘Atlanta’
select sales_person_id from promotions
where sales > 25000 and city = ‘Tampa’
Basically I would need it to show if select statement one, the table would contain user_id and value = 10 if select statement two user_id and value = 5 if select statement three user_id and value = 7
I have tried using case statements with an alias to get a column called value with no luck. Any help would be greatly appreciated.
回答1:
Just convert the conditions in the where clauses to when conditions in a case expression:
SELECT sales_person_id,
CASE WHEN sales > 30000 AND city = 'Georgia' THEN 10
WHEN sales > 50000 and city = 'Atlanta' THEN 7
WHEN sales > 25000 and city = 'Tampa' THEN 5
END AS value
FROM promotions
来源:https://stackoverflow.com/questions/35963293/case-statements-with-alias