Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional statements using HANA SQL Script

Tags:

sql

hana

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')
like image 381
user2446927 Avatar asked Nov 20 '25 04:11

user2446927


1 Answers

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;
like image 160
Jordão Avatar answered Nov 22 '25 17:11

Jordão



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!