How do i get the Count of InventSerialId from InventDim - axapta

How do i create a query or using select to get the count of InventSerialId base on a given Itemid, InventLocationId and where the inventSum.PhysicalInvent > 0 or inventSum.Picked > 0.

This is not directly possible using X++.
Consider:
static void _TestDim(Args _args)
{
ItemId itemId = '123';
InventSum inventSum;
InventDim inventDim;
Query q = new Query();
QueryBuildDataSource ds = q.addDataSource(tableNum(InventSum), 's');
QueryRun qr;
;
// ds.addRange(fieldNum(InventSum,ItemId)).value(queryValue(itemId));
ds.addRange(fieldNum(InventSum,Closed)).value(queryValue(NoYes::No));
ds.addGroupByField(fieldNum(InventSum,ItemId));
ds.addSelectionField(fieldNum(InventSum,PhysicalInvent),SelectionField::Sum);
ds.addSelectionField(fieldNum(InventSum,Picked),SelectionField::Sum);
q.addHavingFilter(ds, fieldStr(InventSum,PhysicalInvent), AggregateFunction::Sum).value('>0');
// q.addHavingFilter(ds, fieldStr(InventSum,Picked), AggregateFunction::Sum).value('((s.Picked >0)||(s.PhysicalInvent>0))'); // This is not allowed
ds = ds.addDataSource(tableNum(InventDim), 'd');
ds.joinMode(JoinMode::InnerJoin);
ds.relations(true);
ds.addGroupByField(fieldNum(InventDim,InventSerialId));
ds.addRange(fieldNum(InventDim,InventSerialId)).value('>""');
info(q.dataSourceNo(1).toString());
qr = new QueryRun(q);
while (qr.next())
{
inventSum = qr.getNo(1);
inventDim = qr.getNo(2);
info(strFmt('%1 %2: %3 %4', inventSum.ItemId, inventDim.InventSerialId, inventSum.PhysicalInvent, inventSum.Picked));
break;
}
}
Here you aggreate PhysicalInvent and picked, and you can apply a having-filter using the query method addHavingFilter.
However, you cannot have that combined with another having-filter using a SQL or-statement.
If you try with a query expression, you will get a run-time error.
What you can do is create two views with each filter, then combine them using a union view. This is tricky but doable.
The first should select positive PhysicalInvent and the second should select PhysicalInvent == 0 and positive Picked.

Related

how to avoid duplicate values in db context object in asp.net core

ALTER PROC [dbo].[rendertutorials](#courname nvarchar(30))
AS
BEGIN
SELECT
tu.FkCategoryid AS Fcatid
,tu.Title
,tu.content
,tu.Keywords
,tu.Metadata
,tu.tags
,cat.CategoryName
,cat.CourseName
,cat.courseimagepath AS imgpath
FROM GetCategories AS cat
INNER JOIN GetTutorials AS tu ON cat.CategoryId = tu.FkCategoryId
WHERE tu.publish = 1 AND cat.coursename LIKE '%' + #courname + '%'
END
GO
EXEC[rendertutorials] "mvc"
AS you can see the store procedure will produce the correct data. item[0] ... item[n] are different.
public IActionResult Tutorials()
{
//var data = GetTableLastChanges("listoftitiles #title", 1, "#title");
var data = db.getrenderview.FromSqlRaw("rendertutorials #courname={0}", "mvc").ToList();
return View(data);
}
But when I call the stored procedure from code the above data variable contain the same data (from item[0] till item[n]). Why? What did I miss?
use distinct in sql:
SELECT DISTINCT column1, column2, ...
FROM table_name;

How to get the tables from AOT query in ax 2012

I have drop down in one page, I am selecting AOT query in first page then i will click on next button, then it has to show tables related to that query
If you have a query name, you can loop through all of its datasources like this:
str queryName = "ActivityListOpen";
int i, dbcount;
QueryBuildDataSource qbds;
Query query = new Query(queryName);
dbcount = query.dataSourceCount();
for (i = 1; i <= dbcount; i++)
{
qbds = query.dataSourceNo(i);
info(qbds.name());
}
You can also use table() method on QueryBuildDataSource to retrieve table Id.

Search using linq and dropdowns

I got about 5 look-a-like linq querys just like this SortPerson() metod. I'm trying to develop a search using dropdowns where a user can select values from the dropdown and returns the values that are true from one or more dropdowns the user has selected to use.
Is there a simpler way to develop this? help would be much appreciated
public void SortPerson()
{
var personId = ddlPerson.SelectedValue;
var data = new MyModelContext();
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
select d;
if (!String.IsNullOrEmpty(personId))
{
documents = documents.Where(c => c.DocPerson.Equals(personId));
}
rptResult.DataSource = documents.ToList();
rptResult.DataBind();
}
I don't see the point in joining without Where if you still select only one table.
If you want all document in case when Person is not selected, then you can't create much simpler method. You can write it shorter like:
var documents =
from d in data.tblDocuments
join ...
where String.IsNullOrEmpty(personId) || d.DocPerson equals personId
select d;
so you don't need separate if statement.
If you want to use several values from 5 dropdowns and use them as conditions in single query, just add more conditions:
var personId = ddlPerson.SelectedValue;
var someValue = ddlSomeDDL.SelectedValue;
//3 more values from DDL
var documents = from d in data.tblDocuments
join sp in data.tblPersons on d.DocPerson equals sp.PersonId
where (String.IsNullOrEmpty(personId) || sp.PersonId equals personId)
&& (String.IsNullOrEmpty(someValue) || d.SomeColumn equals someValue)
//3 more conditions
select d;

Convert Linq to SQL

I have researched on the net and most result are converting from sql to linq and seldom have linq to sql.
this is the code which I want to convert to SQL :
using (CommerceEntities db = new CommerceEntities())
{
try
{
var query = (from ProductOrders in db.OrderDetails
join SelectedProducts in db.Products on ProductOrders.ProductID
equals SelectedProducts.ProductID
group ProductOrders by new
{
ProductId = SelectedProducts.ProductID,
ModelName = SelectedProducts.ModelName
} into grp
select new
{
ModelName = grp.Key.ModelName,
ProductId = grp.Key.ProductId,
Quantity = grp.Sum(o => o.Quantity)
} into orderdgrp where orderdgrp.Quantity > 0
orderby orderdgrp.Quantity descending select orderdgrp).Take(5);
RepeaterItemsList.DataSource = query;
RepeaterItemsList.DataBind();
}
catch (Exception exp)
{
throw new Exception("ERROR: Unable to Load Popular Items - " +
exp.Message.ToString(), exp);
}
}
You can attempt to run the LINQ statement in LinqPad. For examples on how to use LinqPad, check the answer here.
It has a tab to show the generated SQL statement.
Here's an article on logging in LINQ to SQL. It lets you specify a TextWriter to which to send the query.
Basically, you can write something like this:
db.Log = new System.IO.StreamWriter("linq-to-sql.log") { AutoFlush = true };
... where db is your data context.
In SQL you'd write something like this (although the produced code will look a lot different, since it is auto-generated):
SELECT TOP 5 Products.ModelName, Products.ProductID, SUM(OrderDetails.Quantity) qty
FROM OrderDetails
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY Products.ProductID, Products.ModelName
HAVING qty > 0
ORDER BY qty DESC

Trying to join firstonly using AX query object

My request is that a client wants to put in a date range (typically a month) and pull all general ledger journals that have at least one line posted in that date range. They will post journals in March for January's period for example, and want to know which journals have that data.
The basic idea is LedgerJournalTable to a firstonly LedgerJournalTrans. I'm not the best with the query object. Why isn't my query working? It's returning multiple journals instead of just one. I was thinking I could group by and be OK, but I'd think this would work.
static void Job38(Args _args)
{
Query q;
QueryRun queryRun;
QueryBuildDatasource qbd;
QueryBuildDatasource qbd2;
QueryBuildRange qbr;
LedgerJournalTable ledgerJournalTable;
;
info(strfmt("%1", date2strxpp(str2date('10/01/2011', 213))));
q = new Query();
qbd = q.addDataSource(tablenum(LedgerJournalTable));
qbd2 = qbd.addDataSource(tableNum(LedgerJournalTrans));
qbd2.relations(true);
qbd2.firstOnly(true);
qbd2.joinMode(JoinMode::InnerJoin);
qbr = qbd2.addRange(fieldNum(LedgerJournalTrans, TransDate));
qbr.value(strfmt('(TransDate > %1) && (TransDate < %2)', Date2StrXpp(str2date('10/01/2011', 213)), Date2StrXpp(str2date('10/31/2011', 213))));
queryRun = new QueryRun(q);
while (queryRun.next())
{
ledgerJournalTable = queryRun.get(tableNum(LedgerJournalTable));
info(strfmt("%1", ledgerJournalTable.JournalNum));
}
}
Have you tried JoinMode::ExistsJoin?

Resources