ragethewolf
ragethewolf

Reputation: 158

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.

Upvotes: 1

Views: 223

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270663

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.

Upvotes: 2

Related Questions