I'm using MySQL and I have this kind of table:
table: historic_v1
id | student_id | level1_start_date | level1_score | level2_start_date | level2_score
1 | 2345 | 2016-02-10 | 7.5 | 2016-04-15 | 5
2 | 5634 | 2016-03-05 | NULL | NULL | NULL
3 | 4885 | 2015-12-02 | 6 | 2016-01-05 | NULL
I want to do an Insert in table historic_v2 with a Select in historic_v1, but I only want to have a row in historic_v2 if the value of the selected field in historic_v1 is not NULL.
I mean, the historic_v2 table needs to be like this:
table: historic_v2
id | student_id | level | key | date | score
1 | 2345 | 1 | start date | 2016-02-10 | NULL
2 | 2345 | 1 | score | NULL | 7.5
3 | 2345 | 2 | start date | 2016-04-15 | NULL
4 | 2345 | 2 | score | NULL | 5
5 | 5634 | 1 | start date | 2016-03-05 | NULL
6 | 4885 | 1 | start_date | 2015-12-02 | NULL
7 | 4885 | 1 | score | NULL | 6
8 | 4885 | 2 | start_date | 2016-01-05 | NULL
In this example, the student 2345 will have 4 rows in the historic_v2 table, the student 5634 only 1 row and the student 4885 3 rows.
How can I do this Insert with Select?
P.S.: I know that this structure is not nice, but it's just a small example of what I really need to do, and the solution of this problem will help me a lot.
Thanks in advance!