I have a table with 33 million phone records with the structure below:
ccpf_cnpj ddd telefone tipo origem
11111111111 83 81021111 M SERASA
11111111111 83 87472222 M SERASA
11111111111 83 81023333 M TRANSUNION
11111111111 83 88724444 M TRANSUNION
11111111111 83 87475555 M TRANSUNION
22222222222 43 36271111 F SERASA
22222222222 44 36272222 F SERASA
22222222222 43 36273333 F TRANSUNION
22222222222 43 36284444 F TRANSUNION
33333333333 51 51811111 F SERASA
33333333333 51 56212222 F SERASA
33333333333 51 96213333 M SERASA
I need to "denormalize" this table so that each CPF has a single record, it should look like this:
CCPF_CNPJ DDD_1 TELEFONE_1 TIPO_1 ORIGEM_1 DDD_2 TELEFONE_2 TIPO_2 ORIGEM_2 DDD_3 TELEFONE_3 TIPO_3 ORIGEM_3 DDD_4 TELEFONE_4 TIPO_4 ORIGEM_4 DDD_5 TELEFONE_5 TIPO_5 ORIGEM_5
11111111111 83 81021111 M SERASA 83 87472222 M SERASA 83 81023333 M TRANSUNION 83 88724444 M TRANSUNION 83 87475555 M TRANSUNION
22222222222 43 36271111 F SERASA 44 36272222 F SERASA 43 36273333 F TRANSUNION 43 36274444 F TRANSUNION NULL NULL NULL NULL
33333333333 51 51811111 F SERASA 51 56212222 F SERASA 51 96213333 M SERASA NULL NULL NULL NULL NULL NULL NULL NULL
A colleague here suggested using a Pivot + Unpivot, but I could not adapt to the need, or maybe I do not know how to use the Pivot for this case, usually when I use a field that defines the columns, which is not the case .
I was able to do a sampling (100 records) using joins with the table itself, but the solution became unfeasible due to the volume of data (remembering: 33 million records).
Has anyone ever had anything similar?
The SQL Server version here is 2014.
Thank you in advance.