Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

sql - Categorizing data in select query

So I have a table with 600 thousand rows. We are pulling the data from oracle and transferring it to MSSQL(As we have only read rights in oracle).

Currently it is a simple query

select Name, address,country from Table a

I am looking for a way to add a column in this select statement naming it as Region to categorize the country.

If country in India, China and Sri Lanka then APAC. If country in USA, BRA or CAN then AMER.

My knowledge in SQL is at beginner level so I guess it is not possible and I might need to use support from python to execute this but thought of asking before taking a decision.

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would use a case expression:

select a.*,
       (case when country in ('India', 'China', 'Sri Lanka')
             then 'APAC'
             when country in ('USA', 'BRA', 'CAN')
             then 'AMER'
        end) as region
from a;

I would recommend, though, that region be a column in the reference table, so it is consistent wherever used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...