I have a table and want to convert a column of type decimal to an integer based on a condition. I need to do this as performant as possible.
My currently working query looks like this:
select *, (case when C_CUSTKEY < 20 then 1 else 0 end) as bit
from TPCH.CUSTOMER
However I'm trying to improve this. Is it possible to do the conversion on the fly, e.g. something like this:
select *, cast((C_CUSTKEY < 20) as integer) as bit
from TPCH.CUSTOMER
Or even for a simpler version like:
select *, (C_CUSTKEY < 20) as bit
from TPCH.CUSTOMER
No matter what functionality I use, I always get the following error:
sql syntax error: incorrect syntax near "<"
Update 1
So for better understanding an actual usecase of what I want to do is as following:
select ( (case when col1 < x then 1 else 0 end)
+ (case when col2 > y then 2 else 0 end) ) as bitset
from TPCH.CUSTOMER
In general there can be a large number of 'case when' expressions (>100).
- Now the first problem is that the expression is very slow and I need to improve performance so I wanted to know if its possible to to the conversion on the fly smth like
2 * to_int(col2 > y)
but I cannot find a way to do this. - Second problem is when I have many 'case when' expressions then I get the following error:
SQL internal parse tree depth exceeds its maximum: parse tree depth exceeds its maximum:255