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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

loop select sum()

(0) ShareShare
ReportReport
Posted on by

Hi all, I need to do a SELECT using a sum function and also get one more field in the SELECT, but I have some problem that prevents the second field from being displayed in the result. Here is an example:

while select sum(AvailPhysical), ItemId
        from inventSum
        where (!inventSum.Closed) && (!inventSum.ClosedQty) &&
              (inventSum.AvailPhysical != 0)

        join sumInventDim
        group by InventLocationId, wMSLocationId, InventBatchId, InventSerialId
        where (sumInventDim.InventDimId == inventSum.InventDimId) &&
              (sumInventDim.InventLocationId == '004') &&
              (sumInventDim.wMSLocationId == '5-A-01/1')

        exists join wmsLocation
        where (wmsLocation.InventLocationId == sumInventDim.InventLocationId) &&
              (wmsLocation.wMSLocationId == sumInventDim.wMSLocationId) &&
              ((wmsLocation.locationType == WMSLocationType::Buffer) ||
              (wmsLocation.locationType == WMSLocationType::Pick))
    {
        info(strfmt("Item: %1 - Qtd: %2",inventSum.ItemId, inventSum.AvailPhysical));
    }

2654.log.PNG

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    You are missing a group by itemId, since you have to group with aggregate functions.

    static void Job123(Args _args)
    {
        InventSum           inventSum;
        InventDim           sumInventDim;
        WMSLocation         wmsLocation;

        while select sum(AvailPhysical), ItemId
            from inventSum
                group by ItemId
                where  inventSum.ItemId         == 'myitem123'
                    && inventSum.Closed         == NoYes::No
                    && inventSum.ClosedQty      == NoYes::No
                    && inventSum.AvailPhysical  != 0
            join sumInventDim
                group by InventLocationId, wMSLocationId, InventBatchId, InventSerialId
                where  sumInventDim.InventDimId == inventSum.InventDimId
                    //&& (sumInventDim.InventLocationId == '004')
                    //&& (sumInventDim.wMSLocationId == '5-A-01/1')
            exists join wmsLocation
                where  wmsLocation.InventLocationId == sumInventDim.InventLocationId
                    && wmsLocation.wMSLocationId    == sumInventDim.wMSLocationId
                    && (wmsLocation.locationType    == WMSLocationType::Buffer
                        || wmsLocation.locationType == WMSLocationType::Pick)
        {
            info(strfmt("Item: %1 - Qty: %2",inventSum.ItemId, inventSum.AvailPhysical));
        }
    }


  • Community Member Profile Picture
    on at

    Error: The InventLocationId column has been specified more than one time in the ORDER BY list. The columns in the ORDER BY list must be unique.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    See the code above, works for me fine. I have added ItemId filter so it does not loop through all items.

  • Community Member Profile Picture
    on at

    Thanks to everyone, I really needed to add the ItemId, but after that I needed to put the names of the tables before each field name in the groupby clause.

    Problem solved, thank you!

  • Vilmos Kintera Profile Picture
    46,149 on at

    Please mark all helpful answers as verified, especially that I took the time to fix and format your code, thanks :)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans