Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...