Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
marciomgm
Creator
Creator

Script para converter registros de tabelas em colunas

Olá estou tentando desenvolver um script de carga de dados com única tabela de produtos adicionando novas colunas com base no resultado de 2 tabelas (anexos e imagem_externa)

De acordo com a figura, o modelo apresenta as tabelas onde cada produto pode ter até 3 registros de anexos e até 3 registros de url

O resultado final seria uma tabela contendo os valores de ambas as tabelas anexos e imagem_externa concatenados em uma única linha com a tabela produto

id nome anexo_1 anexo_2 anexo_3 url_1 url_2 url_3

 

 

Screen Shot 2023-08-08 at 14.54.04.png

 

 

Labels (2)
1 Solution

Accepted Solutions
pablolabbe
Luminary Alumni
Luminary Alumni

You can solve this using join considering the fields starting with __Key are sequential for each unique id. see the following example:

anexos:

id anexo __KEY_produto
3461 f02kr093f930fk3k 1
3461 vvopsdmvpvmpos 2
3461 vldspvsdvm 3
6524 ddvvsvsvdsv 1

 

you can solve this using a sequence os joins with a filter and to have a code more flexible and add more colums, the join is inside a for loop.

anexo:
load * inline [
id,anexo,__KEY_produto
3461, f02kr093f930fk3k , 1
3461, vvopsdmvpvmpos,2
3461, vldspvsdvm,3
6524, ddvvsvsvdsv, 1
1524, c9s0c90s, 1
];


Produto:
Load  * inline [
id, nome, codigo
3461, prod1 , A124
6524, prod2, B4560
1524, prod3 ,CA124
];

for vkey =  1 to 3 
  Left Join (Produto)
  Load id, anexo as anexo_$(vkey)
  Resident anexo
  where __KEY_produto = $(vkey);
next vkey;


drop table anexo;

 This will be the final result

resultado unpivot.png

Hope this helps anyone else with similar use case.

Best Regards,

Pablo

View solution in original post

2 Replies
cristianj23a
Partner - Creator III
Partner - Creator III

Hi, to convert records into columns you must use the Cross Table.
Here is a URL that explains it with examples:

https://help.qlik.com/en-US/sense/May2023/Subsystems/Hub/Content/Sense_Hub/Scripting/ScriptPrefixes/...

 

Regarts.

https://www.linkedin.com/in/cristianjorge/
Do not forget to mark as "Accepted Solution" the comment that resolves the doubt.
pablolabbe
Luminary Alumni
Luminary Alumni

You can solve this using join considering the fields starting with __Key are sequential for each unique id. see the following example:

anexos:

id anexo __KEY_produto
3461 f02kr093f930fk3k 1
3461 vvopsdmvpvmpos 2
3461 vldspvsdvm 3
6524 ddvvsvsvdsv 1

 

you can solve this using a sequence os joins with a filter and to have a code more flexible and add more colums, the join is inside a for loop.

anexo:
load * inline [
id,anexo,__KEY_produto
3461, f02kr093f930fk3k , 1
3461, vvopsdmvpvmpos,2
3461, vldspvsdvm,3
6524, ddvvsvsvdsv, 1
1524, c9s0c90s, 1
];


Produto:
Load  * inline [
id, nome, codigo
3461, prod1 , A124
6524, prod2, B4560
1524, prod3 ,CA124
];

for vkey =  1 to 3 
  Left Join (Produto)
  Load id, anexo as anexo_$(vkey)
  Resident anexo
  where __KEY_produto = $(vkey);
next vkey;


drop table anexo;

 This will be the final result

resultado unpivot.png

Hope this helps anyone else with similar use case.

Best Regards,

Pablo