Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

rewriting T-SQl query with having clause to AX programmatic query

(0) ShareShare
ReportReport
Posted on by 768
Hello,
 
I have rewritten this t-sql query 
 
select ConnectionId, ContractNum, min(ENDDATETIME) from MCSCMBILAGREEMENT
where startdatetime <> enddatetime
group by ConnectionId, ContractNum
having count(ConnectionId) > 1
order by min(ENDDATETIME) desc
 
to AX programmatic query

static void Job10(Args _args)
{
    Query       query             = new Query();
    QueryRun    queryRun;
    QueryBuildDataSource    qbdsMcsCmBilAgreement; 
    McsCmBilAgreement       agreement, agreement2;
    
    qbdsMcsCmBilAgreement = query.addDataSource(tableNum(McsCmBilAgreement));
    
    qbdsMcsCmBilAgreement.addSortField(fieldNum(McsCmBilAgreement,StartDateTime),SortOrder::Ascending);
    
    qbdsMcsCmBilAgreement.addGroupByField(fieldNum(McsCmBilAgreement, connectionid));
    qbdsMcsCmBilAgreement.addGroupByField(fieldNum(McsCmBilAgreement, contractnum));
    
    query.addHavingFilter(qbdsMcsCmBilAgreement, fieldStr(McsCmBilAgreement, connectionid), AggregateFunction::Count).value("> 1");
    
    info(query.toString());
    queryRun = new QueryRun(query);
    
    while(queryRun.next())
    {
        agreement = queryRun.get(tableNum(McsCmBilAgreement));
        
        info(strFmt("%1 %2 %3 %4", agreement.ConnectionId, agreement.ContractNum, agreement.StartDateTime, agreement.EndDateTime));
      
    }
}
 
 
how to add this clause where startdatetime <> enddatetime to the AX query from above?
 
thanks.
Categories:
  • Suggested answer
    André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,820 Super User 2024 Season 2 on at
    rewriting T-SQl query with having clause to AX programmatic query
    Hi Dark Knight,

    You can add a range on the field 'RecId' and add as query value an SQL statement between brackets like:
     
    (McsCmBilAgreement.StartDataTime != McsCmBilAgreement.EndDateTime)

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans