Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

filter a query after setting range

Posted on by 45

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() ;

}

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: filter a query after setting range

    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?

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: filter a query after setting range

    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);

  • m.salah297 Profile Picture
    m.salah297 45 on at
    RE: filter a query after setting range

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

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: filter a query after setting range

    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
    m.salah297 45 on at
    RE: filter a query after setting range

    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
  • m.salah297 Profile Picture
    m.salah297 45 on at
    RE: filter a query after setting range

    Dear Martin;

    I will try it and give u my feedback.

  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: filter a query after setting range

    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
    m.salah297 45 on at
    RE: filter a query after setting range

    Thanks i'll give attention to that next time.

  • Martin Dráb Profile Picture
    Martin Dráb 230,188 Most Valuable Professional on at
    RE: filter a query after setting range

    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();
    	}
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans