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

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Suggested answer
    Rustem Galiamov Profile Picture
    8,072 on at

    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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 21

#2
Jagadabi Profile Picture

Jagadabi 6

#2
ZvikaR Profile Picture

ZvikaR 6

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans