I have two tables. Lets call them Table1 and Table2. I need to create in the code query like.
Select * from Table1
exists join Table2 where
Table1.Key == Table2.Key &&
(Table1.A == '1' || Table2.C == '2')
So to transfer to code I tried:
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
Table1 test;
QueryRun qr;
q = new Query();
qbd = q.addDataSource(tableNum(Table1));
qbd .name('Table1');
ofQbds = qbd.addDataSource(tableNum(Table2));
ofQbds.name('Table2');
ofQbds.addLink(fieldNum(Table1, Key), fieldNum(Table2, Key));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1',
ofQbds.name(), 'C', '5');
info(range);
ofQbds.addRange(fieldName2id(tableNum(Table1 ), 'A')).value(range);
qr = new QueryRun(q);
while (qr.next())
{
test = qr.get(test.TableId);
info(test.A + ','+test.Key);
}
info('done');
But the or condition does not work properly.
For example If I have values
Table1
A,Key
1,1
2,2
3,3
Table2
C,Key
2,2
I would expect 2 results from Table1
1,1
2,2
but I get only the first one
Message
Table1.A == 1 OR Table2.C == 2
2,2
done
I tried different joinModes and switching || and OR but nothing I tried yet works.
What I need to change to be able to have or condition in the code?
Try changing this line:
range = strFmt('%1.%2 == %3 OR %4.%5 == %6', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
to
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))', qbd.name(), 'A', '1', ofQbds.name(), 'C', '5');
Don't use sql "or", instead use the x++ "||"
Edit: see http://www.axaptapedia.com/Expressions_in_query_ranges for more information.
Edit 2: This code executes for me in the desired fashion. I would recommend using the most up to date compile-time functions such as tableNum() and fieldNum() etc.
Query q;
QueryBuildDataSource qbd;
QueryBuildDataSource ofQbds;
QueryBuildRange qbr;
str range;
QueryRun qr;
CustTable custTable;
CustGroup custGroup;
q = new Query();
qbd = q.addDataSource(tableNum(CustTable));
ofQbds = qbd.addDataSource(tableNum(CustGroup));
ofQbds.addLink(fieldNum(CustTable, CustGroup), fieldNum(CustGroup, CustGroup));
ofQbds.joinMode(JoinMode::InnerJoin);
range = strFmt('((%1.%2 == %3) || (%4.%5 == %6))',
tableStr(CustTable),
fieldStr(CustTable, AccountNum),
'000001',
tableStr(CustGroup),
fieldStr(CustGroup, CustGroup),
'UAC');
info(range);
ofQbds.addRange(fieldNum(CustTable, AccountNum)).value(range);
qr = new QueryRun(q);
while (qr.next())
{
custTable = qr.get(tableNum(CustTable));
info(custTable.AccountNum + ',' +custTable.CustGroup);
}
Related
How can I translate this to a lambda expression? Can anyone please help?
SELECT
p.*
FROM
db.MainProduct p
WHERE
p.id IN (SELECT ms.MainProductId
FROM db.MainProductToSupplierProduct ms)
AND (p.description LIKE '%' + #filter + '%'
OR Coalesce(#filter,'') = '')
Or in query syntax:
var ans = from p in db.MainProduct where (from ms in db.MainProductToSupplierProduct select ms.MainProductId).Contains(p.id) && (filter == "" || p.description.Contains(filter)) select p;
Not sure that is the most efficient way, a less literal translation would use a join:
var ans = from p in db.MainProduct join ms in db.MainProductToSupplierProduct on p.id equals ms.MainProductId where filter == "" || p.description.Contains(filter) select p;
This is a left semi join and the best way to implement that is probably an EXISTS in SQL - it may be faster than join:
var ans = from p in db.MainProduct where db.MainProductToSupplierProduct.Any(ms => ms.MainProductId == p.id) && (filter == "" || p.description.Contains(filter)) select p;
Below code can help you out
var filteredMainProduct= MainProduct.Where(t => MainProductToSupplierProduct.Any(z => z.MainProductId == t.id) && #filter != null ? t.description.Contains(#filter) : t.FareBasisDescription.Contains(""));
V_SQL4 := 'UPDATE EMP_TABLE m
Set m.name = mft.name,
m.age = mft.age,
m.dept = mft.dept,
Where m.id = mft.id and
(m.name != mft.name Or
m.age != mft.age Or
m.dept != mft.dept )';
DBMS_OUTPUT.PUT_LINE(V_SQL4);
EXECUTE IMMEDIATE V_SQL4;
How and where to declare the temporary table EMP_TMPas mft in the statement?
If i look into the requirement i dont see requirment of PL/SQL in
this. A better approach woould be using Merge. I have illuistrated an
example below. If Dynamic SQL is not hard and bound you can use this
too. Let me know if this helps.
MERGE INTO EMP_TABLE m USING EMP_TMP mft
ON (m.id = mft.id AND (m.name != mft.name OR m.age != mft.age OR m.dept != mft.dept))
WHEN MATCHED THEN
UPDATE SET
m.name = mft.name,
m.age = mft.age,
m.dept = mft.dept;
This SO post has an answer to a similar question.
In your case, the query would transform as below
V_SQL4 := 'UPDATE EMP_TABLE m
SET (name, age, dept) = (SELECT mft.name
,mft.age
,mft.dept
FROM EMP_TMP mft
WHERE m.id = mft.id
AND m.name != mft.name Or
AND m.age != mft.age Or
AND m.dept != mft.dept
)
WHERE EXISTS (
SELECT 1
FROM EMP_TMP mft
WHERE m.id = mft.id
)';
I have 2 queries and calling a function 2 times I need call the function one time only based on msg_sys_no count and msg_trans_type.
please find the queries mentioned below and provide me the solution for merging into single.
SELECT COUNT(DISTINCT b1.msg_sys_no) INTO A
FROM tra_message b1
WHERE TO_CHAR(b1.msg_when_created,'YYYY-MM-DD') = in_start_date
AND b1.msg_service_provider = in_svc_provider
AND b1.msg_trans_type = 'TRADE1'
AND get_transaction_status_func(b1.msg_sys_no, b1.msg_trans_type) = 'S';
SELECT COUNT(DISTINCT b1.msg_sys_no) INTO B
FROM tra_message b1
WHERE TO_CHAR(b1.msg_when_created,'YYYY-MM-DD') = in_start_date
AND b1.msg_service_provider = in_svc_provider
AND b1.msg_trans_type = 'TRADE2'
AND get_transaction_status_func(b1.msg_sys_no, b1.msg_trans_type) = 'S';
What about something like this:
WITH tra_data
AS (SELECT *
FROM tra_message
WHERE TO_CHAR (msg_when_created, 'YYYY-MM-DD') = in_start_date
AND msg_service_provider = in_svc_provider
AND get_transaction_status_func (msg_sys_no, msg_trans_type) =
'S')
SELECT COUNT (*)
FROM tra_data
WHERE msg_trans_type = 'TRADE1'
UNION
SELECT COUNT (*)
FROM tra_data
WHERE msg_trans_type = 'TRADE2'
The problem is your AND b1.msg_trans_type IN ('TRADE1','TRADE2') condition.
Try something like this:
select COUNT(DISTINCT a) TRADE1,
COUNT(DISTINCT b) TRADE2
into A,B
from (
select case when b1.msg_trans_type = 'TRADE1'
then b1.msg_sys_no
else null end as a,
case when b1.msg_trans_type = 'TRADE2'
then b1.msg_sys_no
else null end as b
FROM tra_message b1
WHERE TO_CHAR(b1.msg_when_created,'YYYY-MM-DD') = in_start_date
AND b1.msg_service_provider = in_svc_provider
AND b1.msg_trans_type IN ('TRADE1','TRADE2')
AND get_transaction_status_func(b1.msg_sys_no, b1.msg_trans_type) = 'S'
);
This code should be able to run on any AX system (change item & warehouse). The issue is that CustInvoiceJour is not being returned sometimes. What is odd, is if I just do a "select custInvoiceJour" with the same relations, it DOES find it? How is this possible?
static void Job59(Args _args)
{
CustInvoiceTrans custInvoiceTrans;
CustInvoiceJour custInvoiceJour;
InventTable inventTable;
QueryBuildRange rangeItem;
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildDataSource qbds3;
QueryRun qr;
;
query = new Query();
qbds1 = query.addDataSource(tablenum(CustInvoiceTrans));
qbds2 = qbds1.addDataSource(tablenum(CustInvoiceJour));
qbds2.relations(true);
qbds2.addRange(fieldnum(CustInvoiceJour, SalesType)).value('!' + enum2str(SalesType::ReturnItem));
rangeItem = qbds1.addRange(fieldnum(CustInvoiceTrans, ItemId));
qbds1.addRange(fieldnum(CustInvoiceTrans, InvoiceDate)).value(queryRange(#'8/1/2011', #'8/31/2011'));
qbds2.orderMode(OrderMode::OrderBy);
qbds2.addOrderByField(fieldnum(CustInvoiceJour, DlvCountryRegionId));
qbds2.addOrderByField(fieldnum(CustInvoiceJour, DlvState));
qbds3 = qbds1.addDataSource(tablenum(InventDim));
qbds3.relations(true);
qbds3.joinMode(JoinMode::ExistsJoin);
qbds3.addRange(fieldnum(InventDim, InventLocationId)).value(SysQuery::value('FG'));
select firstonly inventTable
where inventTable.ItemId == '44831';
info (strfmt("%1", inventTable.ItemId));
rangeItem.value(inventTable.ItemId);
qr = new QueryRun(query);
while (qr.next())
{
custInvoiceTrans = qr.get(tablenum(CustInvoiceTrans));
custInvoiceJour = qr.get(tablenum(CustInvoiceJour));
if (!custInvoiceJour)
{
info ("Not found");
select firstonly custInvoiceJour
where custInvoiceJour.SalesId == custInvoiceTrans.SalesId &&
custInvoiceJour.InvoiceId == custInvoiceTrans.InvoiceId &&
custInvoiceJour.InvoiceDate == custInvoiceTrans.InvoiceDate &&
custInvoiceJour.numberSequenceGroup == custInvoiceTrans.numberSequenceGroup;
if (custInvoiceJour)
info("Found it");
}
}
}
The debugger shows these as the queries:
NAME:
qbds1
VALUE:
SELECT * FROM CustInvoiceTrans ORDER BY CustInvoiceJour.DlvCountryRegionId ASC, CustInvoiceJour.DlvState ASC WHERE ((ItemId = N'44831')) AND ((InvoiceDate>={ts '2011-08-01 00:00:00.000'} AND InvoiceDate<={ts '2011-08-31 00:00:00.000'})) EXISTS JOIN * FROM InventDim WHERE CustInvoiceTrans.InventDimId = InventDim.inventDimId AND ((InventLocationId = N'FG'))
NAME:
qbds2
VALUE:
SELECT * FROM CustInvoiceJour WHERE CustInvoiceTrans.SalesId = CustInvoiceJour.SalesId AND CustInvoiceTrans.InvoiceId = CustInvoiceJour.InvoiceId AND CustInvoiceTrans.InvoiceDate = CustInvoiceJour.InvoiceDate AND CustInvoiceTrans.numberSequenceGroup = CustInvoiceJour.numberSequenceGroup AND ((NOT (SalesType = 4)))
NAME:
qbds3
VALUE:
SELECT * FROM InventDim WHERE CustInvoiceTrans.InventDimId = InventDim.inventDimId AND ((InventLocationId = N'FG'))
Your qbds1 value doesn't show JOIN * FROM CustInvoiceJour WHERE ... (joined qbds2).
Add qbds2.fetchMode(QueryFetchMode::One2One); to your code and everything should be alright.
P.S. I would suggest to do it the other way round: CustInvoiceJour in qbds1, and CustInvoiceTrans in qbds2 (with default fetchMode), then you wouldn't have this problem at all.
Using LINQ to SQL
db.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList();
executes
SELECT [t1].[ID], [t1].[CategoryID], [t1].[Name], [t1].[Price], [t1].[Descripti
n], [t1].[IsFeatured], [t1].[IsActive]
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID], [t0].[CategoryID], [t0].[Name
, [t0].[Price], [t0].[Description], [t0].[IsFeatured], [t0].[IsActive]) AS [ROW
NUMBER], [t0].[ID], [t0].[CategoryID], [t0].[Name], [t0].[Price], [t0].[Descrip
ion], [t0].[IsFeatured], [t0].[IsActive]
FROM [dbo].[Products] AS [t0]
WHERE [t0].[ID] = #p0
) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN #p1 + 1 AND #p1 + #p2
ORDER BY [t1].[ROW_NUMBER]
-- #p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- #p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- #p2: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
It's using ROW_NUMBER for pagination... good.
Now, I'm trying to use relationships generated by LINQ to SQL to paginate data. Using the query...
var cat = db.Categories.Where(c => c.ID == 1).SingleOrDefault();
cat.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList();
SELECT [t0].[ID], [t0].[Name]
FROM [dbo].[Categories] AS [t0]
WHERE [t0].[ID] = #p0
-- #p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
SELECT [t0].[ID], [t0].[CategoryID], [t0].[Name], [t0].[Price], [t0].[Descriptio
n], [t0].[IsFeatured], [t0].[IsActive]
FROM [dbo].[Products] AS [t0]
WHERE [t0].[CategoryID] = #p0
-- #p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
Now the use of ROW_NUMBER and pagination is gone... it's getting all Products where CategoryID = 1... why is it getting ALL rows?
I think its because the category is in memory. You are asking it, implicitly, to get the products of the category. This implicit request for data is for filled, and then in memory (where the category is at this point) the query is executed.
I'm thinking its equivalent to :
var cat = db.Categories.Where(c => c.ID == 1).SingleOrDefault();
var prods = db.Products.Where(c => c.ID == 1).ToList();
var r = prods.Where(p.CategoryID == cat.ID).Skip(1).Take(1);
Note the significance, what if cat changes in memory? The size of the collection could vary.
NOTE: Thanks for the headache :)
have you tried:
var cat = db.Categories.Where(c => c.ID == 1);
var prod = cat.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList();
You haven't assigned the output of your second LINQ query. So 'cat' is still the first query only.