Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
MalcolmCICWF
Creator III
Creator III

Keeping only the Record that has the Max Create Date

I want to left join a new table to a current table I have. My new table I am pulling from an existing Qvd, has multiple records for an account number. What is the easiest way, in my report, to only pull the record that is the max CreatedDate, for each unique account number? I need the status (3rd column) from that most recent record.

Capture.JPG.jpg

9 Replies
anbu1984
Master III
Master III

Load AcctNo,FirstSortedValue(Status,-Timestamp#(CreatedDate,'YYYY-MM-DD hh:mm:ss.fff')) From Qvd Group by AcctNo

maxgro
MVP
MVP

t:          // test data

load AcctNo, Timestamp#(CreatedDate,'YYYY-MM-DD hh:mm:ss.fff') as CreatedDate, Status inline [

AcctNo, CreatedDate, Status

1, 1-1-2013 22:27:48610, a

1, 1-1-2015 22:27:48611, b

2, 1-1-2012 22:27:48610, c

2, 1-1-2010 22:27:48610, d

2, 1-8-2010 22:27:48610, e

2, 1-10-2010 22:27:48611, f

];

t2:

NoConcatenate

load *

Resident t

where peek(AcctNo) <> AcctNo                // first AcctNo by CreatedDate desc

order by AcctNo, CreatedDate desc;

DROP Table t;

MalcolmCICWF
Creator III
Creator III
Author

I tried this, but it is saying invalid expression. Maybe I did not set it up properly, but I looks like I did. Am I missing something?


LOAD

EXTR_ACCT_ID,FirstSortedValue(EXTR_ID,-Timestamp#(EXTR_CREATEDDATE,'YYYY-MM-DD hh:mm:ss.fff')) ,

EXTR_ID as Collect_Score

FROM [..\..\..\Qvd Applications\QVD_EXTR.qvd](qvd)

Group by EXTR_ACCT_ID;

 

MarcoWedel

Hi,

EXTR_ID is missing in the group by clause:



LOAD

EXTR_ACCT_ID,FirstSortedValue(EXTR_ID,-Timestamp#(EXTR_CREATEDDATE,'YYYY-MM-DD hh:mm:ss.fff')) ,

EXTR_ID as Collect_Score

FROM [..\..\..\Qvd Applications\QVD_EXTR.qvd](qvd)

Group by EXTR_ACCT_ID, EXTR_ID;



Hope this helps


regards


Marco

jagan
Luminary Alumni
Luminary Alumni

Hi,

Try like this

Data:

LOAD

*

WHERE Flag = 1;

LOAD

*,

If(peek(AcctNo) <> AcctNo, 1, 0) AS Flag

FROM DataSource

ORDER BY AccountNumber, Date Desc;

Hope this helps you.

Regards,

Jagan.

jyothish8807
Master II
Master II

Hi,

Try this:

Data:

LOAD

*

WHERE Flag = 1;

LOAD

*,

If((AcctNo) <> previous(AcctNo), 1, 0) AS Flag

FROM DataSource

Order by   AccountNumber,Date Desc;

Regards

KC

Best Regards,
KC
anbu1984
Master III
Master III

If you are looking for only status for maximum date per Account number, then remove this line from your script

EXTR_ID as Collect_Score

MalcolmCICWF
Creator III
Creator III
Author

can anyone Explain how these are working exactly to pull the most recent? I am interested in understanding how it functions.

anbu1984
Master III
Master III

From QV help

firstsortedvalue ([ distinct ] expression [, sort-weight [, n ]])

Returns the first value of expression sorted by corresponding sort-weight when expression is iterated over a number of records as defined by a group by clause. Sort-weight should return a numeric value where the lowest value will render the corresponding value of expression to be sorted first. By preceding the sort-value expression with a minus sign, the function will return the last value instead. If more than one value of expression share the same lowest sort-order, the function will return NULL. By stating an n larger than 1, the nth value in order will be returned. If the word distinct occurs before the expression, all duplicates will be disregarded.

LOAD

EXTR_ACCT_ID,FirstSortedValue(EXTR_ID,-Timestamp#(EXTR_CREATEDDATE,'YYYY-MM-DD hh:mm:ss.fff'))

FROM [..\..\..\Qvd Applications\QVD_EXTR.qvd](qvd)

Group by EXTR_ACCT_ID;

FirstSortedValue(EXTR_ID,Timestamp#(EXTR_CREATEDDATE,'YYYY-MM-DD hh:mm:ss.fff')) -- Within each group of EXTR_ACCT_ID, EXTR_CREATEDDATE is sorted in ascending order and selects EXTR_ID for the minimum EXTR_CREATEDDATE

FirstSortedValue(EXTR_ID,-Timestamp#(EXTR_CREATEDDATE,'YYYY-MM-DD hh:mm:ss.fff')) -- If minus is used in sort expression then Within each group of EXTR_ACCT_ID, EXTR_CREATEDDATE is sorted in descending order and selects EXTR_ID for the maximum EXTR_CREATEDDATE