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

SQL statement

Hi All,

I have 2 tables like below, i want to get record based on latest date. I have to write this in script to load data from SQL.

tab1:
F1F2F3
101/01/2014test1
202/01/2014test2
203/01/2014test3
204/01/2014test4
305/01/2014test5
306/01/2014test6
307/01/2014test7
308/01/2014test8
409/01/2014test9
410/01/2014test10
511/01/2014test11

Tab 2
F1F2
1Name1
2Name2
3Name3
4Name4
5Name5

Final
F1F2F3F4
101/01/2014test1Name1
204/01/2014test4Name2
308/01/2014test7Name3
410/01/2014test10Name4
511/01/2014test11Name5

Can any one help?

4 Replies
anbu1984
Master III
Master III

Tab1:

Load F1,FirstSortedValue(F2,-F2) As F2,FirstSortedValue(F3,-F2) As F3 Group by F1;

Load F1,Date#(F2,'D/M/YYYY') As F2,F3 Inline [

F1,F2,F3

1,01/01/2014,test1

2,02/01/2014,test2

2,03/01/2014,test3

2,04/01/2014,test4

3,05/01/2014,test5

3,06/01/2014,test6

3,07/01/2014,test7

3,08/01/2014,test8

4,09/01/2014,test9

4,10/01/2014,test10

5,11/01/2014,test11 ];

Join

Tab2:

Load * Inline [

F1,F4

1,Name1

2,Name2

3,Name3

4,Name4

5,Name5 ];

alexandros17
Partner - Champion III
Partner - Champion III

There are several ways to solve the problem, via script or via objects expression, I suggest via script:

Load * resident tab1;

left join

Load max(F2) as F2, '1' as flagMax resident tab1 group by F1;

in this way you will have a table with a new field flagMax to be used in your expression

Do now:

left join

load * resident tab2

now you have added Names.

Your expression will use the flag to show only max values

Let me know

Not applicable
Author

hi

try this

Tab1:

Load

F1,

FirstSortedValue(F2,-F2) As F2,

FirstSortedValue(F3,-F2) As F3

Group by F1;

Load

F1,

date(Date#(F2,'D/M/YYYY')) As F2,

F3 Inline [

F1,F2,F3

1,01/01/2014,test1

2,02/01/2014,test2

2,03/01/2014,test3

2,04/01/2014,test4

3,05/01/2014,test5

3,06/01/2014,test6

3,07/01/2014,test7

3,08/01/2014,test8

4,09/01/2014,test9

4,10/01/2014,test10

5,11/01/2014,test11 ];

Join(Tab1)

Tab2:

Load * Inline [

F1,F4

1,Name1

2,Name2

3,Name3

4,Name4

5,Name5 ];

maxgro
MVP
MVP

another solution

using peek to get only first F1 (order by F2 desc)


RESULT

1.png

SCRIPT

tab1:

load * inline [

F1, F2, F3

1, 01/01/2014, test1

2, 02/01/2014, test2

2, 03/01/2014, test3

2, 04/01/2014, test4

3, 05/01/2014, test5

3, 06/01/2014, test6

3, 07/01/2014, test7

3, 08/01/2014, test8

4, 09/01/2014, test9

4, 10/01/2014, test10

5, 11/01/2014, test11

];

final:

NoConcatenate load

  *

Resident

  tab1

where

  F1 <> peek(F1)

order by F1, F2 desc;

drop table tab1;

tab2:

load F1, F2 as F4 inline [

F1, F2

1, Name1

2, Name2

3, Name3

4, Name4

5, Name5

];