9
I have a chart 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 Pivot for this case, usually when I use a field that defines me columns, which is not the case.
I was able to do it in a sampling (100 records) using joins with the table itself, but the solution became unviable due to the volume of data (remembering: 33 million records).
Someone’s been through something similar?
The SQL Server version here is 2014.
Thank you in advance.
@Gypsy Morrison Mendez I managed to execute the code correcting two details, but while running me returned the message 'An error occurred while executing batch. Error message is: Exception of type 'System.Outofmemoryexception' was thrown. ' after just over 5 minutes of execution. I think the volume of data is really impacting. I will try to "break" the table into smaller parts to see if I can perform. Thanks for the help.
– Cassio Veras
I would do for CNPJ tracks. You tried to edit my answer, right? With the change the answer works for you?
– Leonel Sanches da Silva
@Gypsy Morrison Mendez Thank you so much for your help! I was working to divide the table into smaller parts, but they answered my question in Stack overflow in English, I tested the solution and it worked (you’re right, he used tracks, but it was not necessary to use Pivot): http://stackoverflow.com/questions/35340101/rows-to-Columns-pivot
– Cassio Veras