Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Max Function not working in script

I'm working on an incremental load script and need to obtain the last Primary Key loaded (originates from an identify column in SQL).

In the below script I can not get Max(PriKeyItem) to return any results into the vMaxKey variable.  If I change Max to Count results are obtained.

Item:

LOAD *

FROM (qvd);

MaxPriKey:

LOAD Max('PriKeyItem') AS "maxPK" Resident Item;

LET vMaxKey = FieldValue('maxPK',1);

//LET vMaxKey=peek('maxPK',-1,'MaxPriKey');

Thanks in advance for any suggestions -

Brett

5 Replies
MK_QSL
MVP
MVP

TableName:

LOAD *

FROM (qvd);

MaxPriKey:

LOAD Max(PriKeyItem) AS maxPK Resident TableName;

LET vMaxKey = Peek('maxPK',0,'MaxPriKey');

SunilChauhan
Champion
Champion

dont use single quotes in max function otherwise it will take it as text and max will not work use below

Item:

LOAD *

FROM (qvd);

MaxPriKey:

LOAD Max(PriKeyItem) AS "maxPK" Resident Item;

LET vMaxKey = FieldValue('maxPK',1);

//LET vMaxKey=peek('maxPK',-1,'MaxPriKey');

hope this helps

Sunil Chauhan
Not applicable
Author

Thanks guys.   I could have sworn the count function worked with the single quotes (and returned a value > 1) but maybe not.

preminqlik
Specialist II
Specialist II

if your primary key is in string format then use the following

LOAD Maxstring(PriKeyItem) AS "maxPK" Resident Item;

chematos
Specialist II
Specialist II

Use distinct for the second table, you could be getting more than one value for Max(PriKeyItem)

TableName:

LOAD *

FROM (qvd);

MaxPriKey:

NoConcatenate

LOAD Distinct Max(PriKeyItem) AS maxPK Resident TableName;

LET vMaxKey = Peek('maxPK');

hope this helps.