i have following data set. i am trying to match string in Col = "Str_B_Class" to the entire string in col = "Str_A". The str_A col has data as class:value. if the class is found in Str_A, then sql should get the value after colon into the desired_output_column , otherwise 0
The problem with below code is that if its lets say found 3132, and the value 10, then instead if returning 1 row where 3132 is matched , it is returning it 6 times. so it is exploding the whole dataset. I need some help in matching the string without exploding the whole table.
Customer_ID || Str_A || Str_B_Class || Desired_Output
-------------------------------------------------------------------------------------------
A1 || 121:8|188:8|3123:10|3125:10|3131:10|3132:10 || 3132 || 10
A1 || 121:8|188:8|3123:10|3125:10|3131:10|3132:10 || 3125 || 10
A1 || 121:8|188:8|3123:10|3125:10|3131:10|3132:10 || 4141 || 0
Query:
select
s.Customer_Id,
s.Str_A,
s.Str_B_Class,
case when s.instance_id = s.Str_B_Class
then s.Count_of_instances
else '0'
end AS Desired_Output
from (
select
Customer_Id, Str_A,
Str_B_Class,
explode(str_to_map(Str_A,'[|]','[:]')) as (instance_id, Count_of_instances)
from my_table
) as s