Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Query to Calculate Sum(Qty) from inventTrans Table Based on Reference category in InventTransOrigin

(0) ShareShare
ReportReport
Posted on by 69

I calculated total Sales, Production, Purchase... of specific items Results are accurate in SQL query.. I want to know how can i convert this into AOT query. i found ax does not support subqueries can anyone please help to calculate same in aot. 

declare @startDate date;
declare @endDate date;
set @startDate = '2017-01-01';
set @endDate = '2017-01-31';

select distinct t.itemid,
Sales=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 0 and n.ITEMID =t.ITEMID and
s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate ),

Production=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 2 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate ),
Purch=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 3 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
InventTransaction=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 4 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate ),
InventTransfer=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 6 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
ProdLine=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 8 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
BOMLine=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 9 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
BOMMain=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 10 and n.ITEMID =t.ITEMID and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
InventCounting=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 13 and n.ITEMID =t.ITEMID and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
TransferOrderShip=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 21 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate ),
TransferOrderReceive=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 22 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate),
TransferOrderScrap=(select sum(s.QTY)
from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID
where n.REFERENCECATEGORY = 23 and n.ITEMID =t.ITEMID
and s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate)
from INVENTTRANS t join INVENTTRANSORIGIN o on t.INVENTTRANSORIGIN = o.RECID
where t.ITEMID >= '910091' and t.ITEMID <= '910098'
and t.DATEPHYSICAL >= @startDate and t.DATEPHYSICAL <= @endDate
order by 1

*This post is locked for comments

  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at
    RE: Query to Calculate Sum(Qty) from inventTrans Table Based on Reference category in InventTransOrigin

    Hi!

    It's might be look like this:

    declare @startDate date;

    declare @endDate date;

    set @startDate = '2017-01-01';

    set @endDate = '2017-01-31';

    select distinct t.itemid,

    Sales=(select sum(s.QTY)

    from INVENTTRANS s join INVENTTRANSORIGIN n on s.INVENTTRANSORIGIN = n.RECID

    where n.REFERENCECATEGORY = 0 and n.ITEMID =t.ITEMID and

    s.DATEPHYSICAL >= @startDate and s.DATEPHYSICAL <= @endDate )

    In AX:

    TransDate startDate = 2017\01\01;

    TransDate endDate  = 2017\01\31;

    InventTrans sales;

    InventTransOrigin inventTransOrigin;

    select sum(Qty) from sales

    group by ItemId

    where sales.DatePhysical >= startDate

        && sales.DatePhysical <= endDate

    exists join inventTransOrigin

    where inventTransOrigin.recId == sales.InventTransOrigin

    && inventTransOrigin.ReferenceCategory == InventTransType::Sales

    && inventTransOrigin.ItemId == sales.ItemId;

    .......

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,884 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,748 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans