简体   繁体   中英

Split a row into multiple rows based on a column value

I am trying to split a record in a table to 2 records based on a column value. The input table displays the 3 types of products and their price. For a specific product (row) only its corresponding column has value. The other columns have Null.

My requirement is - whenever the product column value (in a row) is composite (ie has more than one product, eg Bolt + Brush), the record must be split into two rows - 1 row each for the composite product types.

So, in this example, notice how the 2nd row (in the input) gets split into 2 rows -> 1 row for "Bolt" and another for the "Brush", with their price extracted from their corresponding columns (ie in this case, "Bolt" = $3.99 and "Brush" = $6.99)

Note: For composite product values there can be at most 2 products as shown in this example (eg Bolt + Brush)

CustId | Product        | Hammer | Bolt  | Brush
--------------------------------
12345  | Hammer         | $5.99  | Null  | Null  
53762  | **Bolt+Brush** | Null   | $3.99 | $4.99  
43883  | Brush          | Null   | Null  | $4.99  

I have tried creating 2 predetermined records via UNION ALL using a CTE and then main_table Left Outer Join with CTE, so that the join yields 2 records instead.

#CustId   | Product   | Price  #
12345     | Hammer    | $5.99  
**53762** | **Bolt**  | $3.99  
**53762** | **Brush** | $4.99  
43883     | Brush     | $4.99  

This has to be solved by Spark-SQL only.

I think this will work:

select CustId, 'Hammer' as product, Hammer
from t
where Product like '%Hammer%'
union all
select CustId, 'Bolt' as product, Bolt
from t
where Product like '%Bolt%'
union all
select CustId, 'Brush' as product, Brush
from t
where Product like '%Brush%';

This would work also

select custid, product, 
            case when product like '%Hammer%' then hammer 
                 when product like '%Bolt%'   then bolt 
            else brush end as Price from
(select custid, explode(split(product,'\\+')) as product,  hammer, bolt, brush  
from t) x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM