web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

filter a query after setting range

(0) ShareShare
ReportReport
Posted on by 47

Hi All;

i have created this query with select statements before and now i want to use an AOT query and modify it in rdp class for an ssrs report

The Issue : in query rdp functions after i create a range for dates i want to select first only row for each "ADVCheckID" with the max "ADVCHKStepSequence"

---------------------------------------------------------------------------------------------------------

Business Logic :

while Select AdvChk

{

while select firstOnly AdvChekStpActul
order by AdvChekStpActul.ADVCHKStepSequence desc
where AdvChekStpActul.ADVCheckID == AdvChk.ADVCheckID
&& AdvChekStpActul.TransDate <= NotesDate
&& AdvChekStpActul.TransDate
&& AdvChekStpActul.Applied == 1

{

if ( (AdvChekStpActul.ADVCHKStepType == Steptype || !Steptype) && (AdvChekStpActul.ADVCHKStepGroupID == StepGroup) && (AdvChk.ADVCheckGroupID == CheckGroup || !CheckGroup) && (AdvChekStpActul.ADVCHKStepID == StepId || !StepId) )
{
TmpDB.CustAccount = AdvChk.CustAccount ;
TmpDB.CustName = AdvChk.CustName ;
TmpDB.AmountCur = AdvChk.AmountCur ;
TmpDB.ADVNetAmount = AdvChk.ADVNetAmount ;
TmpDB.ADVCheckSource = AdvChk.ADVCheckSource ;
TmpDB.ADVCheckGroupID = AdvChk.ADVCheckGroupID ;

TmpDB.ADVCHKStepType = AdvChekStpActul.ADVCHKStepType ;
TmpDB.ADVCheckID = AdvChekStpActul.ADVCheckID ;
TmpDB.Voucher = AdvChekStpActul.Voucher ;
TmpDB.ADVCHKStepID = AdvChekStpActul.ADVCHKStepID ;
TmpDB.TransDate = AdvChekStpActul.TransDate ;
TmpDB.ADVCHKStepGroupID = AdvChekStpActul.ADVCHKStepGroupID ;
TmpDB.ADVCHKStepSequence = AdvChekStpActul.ADVCHKStepSequence ;
TmpDB.LedgerName = DimensionAttributeValueCombination::getDisplayValue(AdvChekStpActul.LedgerDimension);
TmpDB.LedgerDimension = AdvChekStpActul.LedgerDimension ;
}

-----------------------------------------------------------------------------------------------------

QUERY RDP:

qbds = query.dataSourceTable(tablenum(ADVChecks));
queryBuildDataSource = query.dataSourceTable(tablenum(ADVCHKCheckStepsActual));
qbd = query.dataSourceTable(tablenum(ADVCHKCheckStepsActual));


qr = new QueryRun(query);


if(NotesDate)
{
queryBuildRange = queryBuildDataSource.findRange(fieldnum(ADVCHKCheckStepsActual, TransDate));
if (!queryBuildRange)
{
queryBuildRange = queryBuildDataSource.addRange(fieldnum(ADVCHKCheckStepsActual, TransDate));
}
// If an Notes Date has not been set, then use the parameter value to set it.
if(!queryBuildRange.value())
{
queryBuildRange.value(queryRange(DateTimeUtil::minValue() , NotesDate ));

}
}


queryBuildDataSource.relations(true) ;


qbds.addOrderByField(fieldNum(ADVChecks,ADVCheckID), SortOrder::Descending) ;
queryBuildDataSource.addOrderByField(fieldNum(ADVCHKCheckStepsActual,ADVCHKStepSequence), SortOrder::Descending) ;

// qr = new QueryRun(query);

while (qr.next())
{

TmpDB.clear();

AdvChk = qr.get(tablenum(ADVChecks));
AdvChekStpActul = qr.get(tablenum(ADVCHKCheckStepsActual));



if ( (AdvChekStpActul.ADVCHKStepType == Steptype || !Steptype) && (AdvChekStpActul.ADVCHKStepGroupID == StepGroup) && (AdvChk.ADVCheckGroupID == CheckGroup || !CheckGroup) && (AdvChekStpActul.ADVCHKStepID == StepId || !StepId) )
{
TmpDB.CustAccount = AdvChk.CustAccount ;
TmpDB.CustName = AdvChk.CustName ;
TmpDB.AmountCur = AdvChk.AmountCur ;
TmpDB.ADVNetAmount = AdvChk.ADVNetAmount ;
TmpDB.ADVCheckSource = AdvChk.ADVCheckSource ;
TmpDB.ADVCheckGroupID = AdvChk.ADVCheckGroupID ;

TmpDB.ADVCHKStepType = AdvChekStpActul.ADVCHKStepType ;
TmpDB.ADVCheckID = AdvChekStpActul.ADVCheckID ;
TmpDB.Voucher = AdvChekStpActul.Voucher ;
TmpDB.ADVCHKStepID = AdvChekStpActul.ADVCHKStepID ;
TmpDB.TransDate = AdvChekStpActul.TransDate ;
TmpDB.ADVCHKStepGroupID = AdvChekStpActul.ADVCHKStepGroupID ;
TmpDB.ADVCHKStepSequence = AdvChekStpActul.ADVCHKStepSequence ;
TmpDB.LedgerName = DimensionAttributeValueCombination::getDisplayValue(AdvChekStpActul.LedgerDimension);
TmpDB.LedgerDimension = AdvChekStpActul.LedgerDimension ;

TmpDB.insert() ;

}

I have the same question (0)
  • Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at

    First of all, let me format your code and insert with indentation. Next time, please use Insert > Insert Code (in the rich formatting view) to paste source code.

    Business Logic:

    while select advChk
    {
    	while select firstOnly advChekStpActul
    	order by advChekStpActul.AdvChkStepSequence desc
    	where advChekStpActul.ADVCheckID == advChk.ADVCheckID
    	   && advChekStpActul.TransDate <= NotesDate
    	   && advChekStpActul.TransDate
    	   && advChekStpActul.Applied == 1
    	{
    		if ( (advChekStpActul.AdvChkStepType == stepType || !stepType)
    		  && (advChekStpActul.AdvChkStepGroupID == stepGroup)
    		  && (advChk.ADVCheckGroupID == checkGroup || !checkGroup)
    		  && (advChekStpActul.AdvChkStepID == stepId || !stepId))
    		{
    			tmpDB.CustAccount = advChk.CustAccount;
    			tmpDB.CustName = advChk.CustName;
    			tmpDB.AmountCur = advChk.AmountCur;
    			tmpDB.ADVNetAmount = advChk.ADVNetAmount;
    			tmpDB.ADVCheckSource = advChk.ADVCheckSource;
    			tmpDB.ADVCheckGroupID = advChk.ADVCheckGroupID;
    
    			tmpDB.advChkStepType = advChekStpActul.AdvChkStepType;
    			tmpDB.ADVCheckID = advChekStpActul.ADVCheckID;
    			tmpDB.Voucher = advChekStpActul.Voucher;
    			tmpDB.advChkStepID = advChekStpActul.advChkStepID;
    			tmpDB.TransDate = advChekStpActul.TransDate;
    			tmpDB.advChkStepGroupID = advChekStpActul.AdvChkStepGroupID;
    			tmpDB.advChkStepSequence = advChekStpActul.AdvChkStepSequence;
    			tmpDB.LedgerName = DimensionAttributeValueCombination::getDisplayValue(advChekStpActul.LedgerDimension);
    			tmpDB.LedgerDimension = advChekStpActul.LedgerDimension;
    		}
    	}
    }

    QUERY RDP:

    qbds = query.dataSourceTable(tablenum(ADVChecks));
    queryBuildDataSource = query.dataSourceTable(tablenum(AdvChkCheckStepsActual));
    qbd = query.dataSourceTable(tablenum(AdvChkCheckStepsActual));
    
    qr = new QueryRun(query);
    
    if (notesDate)
    {
    	queryBuildRange = queryBuildDataSource.findRange(fieldnum(AdvChkCheckStepsActual, TransDate));
    	if (!queryBuildRange)
    	{
    		queryBuildRange = queryBuildDataSource.addRange(fieldnum(AdvChkCheckStepsActual, TransDate));
    	}
    	
    	// If an Notes Date has not been set, then use the parameter value to set it.
    	if(!queryBuildRange.value())
    	{
    		queryBuildRange.value(queryRange(DateTimeUtil::minValue(), notesDate));
    	}
    }
    
    queryBuildDataSource.relations(true);
    
    qbds.addOrderByField(fieldNum(ADVChecks, ADVCheckID), SortOrder::Descending);
    queryBuildDataSource.addOrderByField(fieldNum(AdvChkCheckStepsActual, AdvChkStepSequence), SortOrder::Descending);
    
    // qr = new QueryRun(query);
    
    while (qr.next())
    {
    	tmpDB.clear();
    
    	advChk = qr.get(tablenum(ADVChecks));
    	advChekStpActul = qr.get(tablenum(AdvChkCheckStepsActual));
    
    	if ( (advChekStpActul.AdvChkStepType == stepType || !stepType)
          && (advChekStpActul.AdvChkStepGroupID == stepGroup)
    	  && (advChk.ADVCheckGroupID == checkGroup || !checkGroup)
    	  && (advChekStpActul.AdvChkStepID == stepId || !stepId))
    	{
    		tmpDB.CustAccount = advChk.CustAccount ;
    		tmpDB.CustName = advChk.CustName ;
    		tmpDB.AmountCur = advChk.AmountCur ;
    		tmpDB.ADVNetAmount = advChk.ADVNetAmount ;
    		tmpDB.ADVCheckSource = advChk.ADVCheckSource ;
    		tmpDB.ADVCheckGroupID = advChk.ADVCheckGroupID ;
    
    		tmpDB.advChkStepType = advChekStpActul.advChkStepType ;
    		tmpDB.ADVCheckID = advChekStpActul.ADVCheckID ;
    		tmpDB.Voucher = advChekStpActul.Voucher ;
    		tmpDB.advChkStepID = advChekStpActul.advChkStepID ;
    		tmpDB.TransDate = advChekStpActul.TransDate ;
    		tmpDB.advChkStepGroupID = advChekStpActul.advChkStepGroupID ;
    		tmpDB.advChkStepSequence = advChekStpActul.advChkStepSequence ;
    		tmpDB.LedgerName = DimensionAttributeValueCombination::getDisplayValue(advChekStpActul.LedgerDimension);
    		tmpDB.LedgerDimension = advChekStpActul.LedgerDimension ;
    
    		tmpDB.insert();
    	}
    }

  • m.salah297 Profile Picture
    47 on at

    Thanks i'll give attention to that next time.

  • Suggested answer
    Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at

    I would consider using the technique I described in Join first line in AX 2012. It means creating a view returning ADVCheckID and an ID of the highest step for each ADVCheckID. Then you can join AdvChk and AdvChekStpActul records to the view to get additional fields.

  • m.salah297 Profile Picture
    47 on at

    Dear Martin;

    I will try it and give u my feedback.

  • m.salah297 Profile Picture
    47 on at

    Dear Martin;

    Unfortunately that with filter with the highest step sequence in the table before i set the date range through the query

    if i have a table like this

    check idstep sequencetransdate
    a1 1 1/1/2020
    a1 2 28/2/2020
    a1 3 29/3/2020
    a2 1 1/1/2020
    a2 2 1/2/2020
    a2 3 25/2/2020

    i want to 1st filter by the dates through query let's say date<= 28/2/2020 then filter by the highest number sequence, so the result will be:

    check idstep sequencetransdate
    a1 2 28/2/2020
    a2 3 25/2/2020

    but if i filtered first through the view i get

    check idstep sequencetransdate
    a2 3 25/2/2020
  • Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at

    Aha, so you don't want the highest from all. All right, then use the maxOf() aggregation function in your query to get the highest ID in your particular context.

  • m.salah297 Profile Picture
    47 on at

    Thanks martin but could u show me the context and syntax i should put the maxOf() in.

  • Verified answer
    Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at

    Unfortunately I don't have much time at the moment - please use a search engine and/or cross-references to find more details.

    In select statements, simply use maxOf() in the field list: select maxOf(MyField) from...

    When using query classes, use qbds.addSelectionField(fieldNum(MyTable, MyField), SelectionField::Max);

  • Martin Dráb Profile Picture
    239,680 Most Valuable Professional on at

    I see you've created another thread about your problem: populate tmp table from select statement then use it to populate another tmp table using query rdp.

    Does it mean that you gave up the query and you don't need our assistance in thisn thread anymore? Or, on the contrary, does it mean that you ran into some problem with the query? If it's the latter, why don't you explain your problem here before giving up your whole approach?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 658 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 632 Super User 2026 Season 1

#3
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 570

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans