Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
ruben_moya
Creator
Creator

Create a new table with loops and left joins

Dear Experts

I've got two tables. The first one is a table that I receive showing some information about users:

userkeycod
1202Area
2202Area
31031Plaza
41029Plaza
51031Plaza

 

The second one, a master table:

DelAreaPlaza
12021031
22021031
32031031
42021031
52021031
62031031
72051029
82051029
92051029

10

2051029

 

I need to end up showing for each user a all the Del that they belong to.

userDel
11
12
14
15
21
22
23
24
25
31
32
33
34
35
36
47
48
49
410
51
52
53
54
55
56

 

I have already solved this problem with left joins and concatenations.

HOWEVER, I've tried to make the script less confusing using a loop:

tableA:
LOAD
user,
key,
cod
FROM excelfile;

FOR EACH a IN FieldValueList('cod')

let vtest=a;

left Join (tableA)
load
$(vtest) as  key,
Del;
SQL SELECT *
FROM DB

NEXT a;

 

what I have in mind, is for each value of cod (Area or Plaza) store it in a variable and use it to load the name of that dimension in my master table. The problem is that I haven't been able to store the values in the field 'cod' in a variable.

Thanks in advance for your help, i hope i explained myself clearly enough

1 Solution

Accepted Solutions
kaanerisen
Creator III
Creator III

Hi Ruben,

Actually you don't need to make left join and loops to get the expected output. Unpivoting the second table by using crosstable function and make an inner join with first table would be enough 🙂

Master:
CrossTable(cod,key)
LOAD
    Del,
    Area,
    Plaza
FROM [lib://web5]
(html, utf8, embedded labels, table is @2);

Inner Join(Master)
LOAD
    user,
    key,
    cod
FROM [lib://web5]
(html, utf8, embedded labels, table is @1);

drop Fields key,cod from Master;

Output :

Untitled.png

Hope it helps,

View solution in original post

2 Replies
kaanerisen
Creator III
Creator III

Hi Ruben,

Actually you don't need to make left join and loops to get the expected output. Unpivoting the second table by using crosstable function and make an inner join with first table would be enough 🙂

Master:
CrossTable(cod,key)
LOAD
    Del,
    Area,
    Plaza
FROM [lib://web5]
(html, utf8, embedded labels, table is @2);

Inner Join(Master)
LOAD
    user,
    key,
    cod
FROM [lib://web5]
(html, utf8, embedded labels, table is @1);

drop Fields key,cod from Master;

Output :

Untitled.png

Hope it helps,

ruben_moya
Creator
Creator
Author

Thanks a lot for your answer. I wasn't familiar with crosstable function. I've been tryng to look for examples but I still dont really understand how did the inner join and crosstable did that result. Could you give me a few concepts so I can understand it? thanks a lot!