0

I have a table with column c_name, c_services which got values like this:

 c_name  |      c_services 
---------------------------------------           
       A |   Email,Photos,Phone,Text 
       B |                Phone,Text  
       C |              Email,Photos  
       D |              Photos,Phone 

From above table and column (c_services), I am trying to populate data in 5 more columns which are:-

1. Total_Services - based on number of services ex- 4 for c_name 'A'
2. Email - 1 if service present else 0
3. Photos - 1 if service present else 0
4. Phone - 1 if service present else 0
5. Text - 1 if service present else 0

For the new column 'Total_Services', I tried:

array_length(regexp_split_to_table(c_services, ','), 1)  Total_Services
or 
count(regexp_split_to_table(c_services)) Total_Service
or
count(string_split(c_services)) Total_Service

but it is not working. For other column, I am thinking of using a case when to populate 1 or 0 value. Please help.

sudeep
  • 93
  • 2
  • 6
  • 17

1 Answers1

3

Never heard of SOQL before but depending on how predictable and stable c_services column is, you could try this.

select t.*, (t.Email+t.Photos+t.Phone+t.Text) as total_service_count
from (select c_name
            ,case when c_services like '%Email%' then 1 else 0 end as Email
            ,case when c_services like '%Photos%' then 1 else 0 end as Photos
            ,case when c_services like '%Phone%' then 1 else 0 end as Phone
            ,case when c_services like '%Text%' then 1 else 0 end as Text
      from your_table) t;
Radagast
  • 5,102
  • 3
  • 12
  • 27