user3531259
user3531259

Reputation: 3

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.

Upvotes: 0

Views: 250

Answers (1)

podiluska
podiluska

Reputation: 51494

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

Upvotes: 1

Related Questions