Case statements with alias

无人久伴 提交于 2019-12-14 02:56:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!