简体   繁体   中英

Split row into multiple rows in SQL Server for insert

I am trying to create a SQL query that will insert a single row as 2 rows in another table.

My data looks like this:

size | indemnity_factor | monitoring_factor
--------------------------------------------    
0    | 1.00             | 1.5 

The end data looks like this:

id | claim_component_type_code | size | adjustment_factor | valid_from_date
------------------------------------------------------------------------------    
1  | Indemnity                 | 0    | 2.5000000         | 2014-01-01
1  | Monitoring                | 1    | 1.5000000         | 2014-01-01

I want to add an entry of Indemnity and Monitoring for every row in the first data source. I haven't really got an idea how to go about it, would be very appreciative if someone could help. Sorry for the rough data but I can't post images with my reputation apparently.

Thanks in advance.

Use unpivot

select * from 
(select size, indemnity_factor as indemnity, monitoring_factor as monitoring 
     from yourtable) src
unpivot (adjustment_factor for claim_component_type_code in (indemnity, monitoring) ) u

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