SQLTeam.com | Weblogs | Forums

Creating a new column with an if function


#1

Hey,

My knowledge of SQL is pretty limited so would be grateful for any help you can give!

I have the following script:

SELECT
total_bid_strategy_active_spend,
country,
agency_name,
holding_company,
year_week,
advertiser_name,

FROM amitvaria.key_metrics_historical

and am looking to add another column to the data where if the total_bid_strategy_active_spend is greater than 0, to have the entry "1" and if it is equal to 0 to have the entry "0"

Thanks so much for your help!!


#2

Try this:

select total_bid_strategy_active_spend
      ,country
      ,agency_name
      ,holding_company
      ,year_week
      ,advertiser_name
      ,sign(total_bid_strategy_active_spend) as yournewcolumn
  from amitvaria.key_metrics_historical
;

#3

Or Try:

SELECT
       total_bid_strategy_active_spend,
       CASE 
            WHEN total_bid_strategy_active_spend>0 THEN 1 
            ELSE 0 
       END AS is_total_bid_strategy_active_spend_gt_0,
       country, 
       agency_name, 
       holding_company, 
       year_week,
       advertiser_name
FROM amitvaria.key_metrics_historical;

#4

worked perfectly thank you so much!!!