Conditional statements using HANA SQL Script

user2446927 picture user2446927 · Jun 4, 2013 · Viewed 17k times · Source

I have a table in HANA studio, which consists of 5 columns:

  • item
  • value
  • cost
  • Margin
  • ABC_cat

How can I generate a 6th column as Margin Categorization, using an if-else condition?

IF ((([Margin])/[VALUE])*100<((SUM([VALUE])-SUM([COST]))/SUM([VALUE]))*100)
  THEN ('BELOW') 
  ELSE ('ABOVE')

Answer

Jord&#227;o picture Jordão · Jun 28, 2013

You need two things: window functions and a case statement:

select
  item, value, cost, margin, abc_cat,
  case when 
    margin / value < (sum(value) over() - sum(cost) over()) / sum(value) over()
    then 'BELOW' 
    else 'ABOVE' 
  end as "Margin Categorization"
from my_schema.my_table;