嵌套 notExists 连接 X++ (动力AX3.0)

发布于 2024-07-13 17:15:12 字数 2162 浏览 10 评论 0原文

当执行以下代码时:

   select sum(qty) from inventTrans
        index hint TransTypeIdx
        where inventTrans.ItemId        == itemId
           && inventTrans.TransType     == InventTransType::Sales
           && inventTrans.InventDimId   == inventDimId
        notExists join custTable
        where custTable.AccountNum      == inventTrans.CustVendAC
           && custTable.CustGroup       == custGroupId
        notExists join salesTable
        where salesTable.SalesId        == inventTrans.TransRefId
           && salesTable.Extraordinary  == NoYes::Yes;

生成的 sql 将第二个 notExists 连接 (salesTable) 嵌套到第一个 notExists 连接 (custTable) 的 where 子句中。 即

SELECT SUM(A.QTY)
FROM
    INVENTTRANS A
WHERE
    A.DATAAREAID  = 'MyCompany'
AND A.ITEMID      = 'MyItem'
AND A.TRANSTYPE   = 0
AND A.INVENTDIMID = 'SomeValue'
AND NOT EXISTS (SELECT 'x'
                FROM CUSTTABLE C
                WHERE C.DATAAREAID  ='MyCompany'
                AND C.ACCOUNTNUM    =A.CUSTVENDAC
                AND C.CUSTGROUP     ='SomeCustGroup'
                AND NOT EXISTS (SELECT 'x'
                                FROM SALESTABLE B
                                WHERE
                                    B.DATAAREAID    ='MyCompany'
                                AND B.SALESID       =A.TRANSREFID
                                AND B.EXTRAORDINARY =1))

有没有办法编写 X++ 选择查询来 notExists 将 salesTable 连接到 inventTrans 表而不是 custTable,这样生成的 SQL 将类似于以下内容?

SELECT SUM(A.QTY)
FROM
    INVENTTRANS A
WHERE A.DATAAREAID     = 'MyCompany'
AND A.ITEMID           = 'MyItem'
AND A.TRANSTYPE        = 0
AND A.INVENTDIMID      = 'SomeValue'
AND NOT EXISTS (SELECT 'x'
                FROM CUSTTABLE C
                WHERE C.DATAAREAID  ='MyCompany'
                AND C.ACCOUNTNUM    =A.CUSTVENDAC
                AND C.CUSTGROUP     ='SomeCustGroup')
AND NOT EXISTS (SELECT 'x'
                FROM SALESTABLE B
                WHERE
                    B.DATAAREAID='MyCompany'
                AND B.SALESID=A.TRANSREFID
                AND B.EXTRAORDINARY=1)

When the following code executes:

   select sum(qty) from inventTrans
        index hint TransTypeIdx
        where inventTrans.ItemId        == itemId
           && inventTrans.TransType     == InventTransType::Sales
           && inventTrans.InventDimId   == inventDimId
        notExists join custTable
        where custTable.AccountNum      == inventTrans.CustVendAC
           && custTable.CustGroup       == custGroupId
        notExists join salesTable
        where salesTable.SalesId        == inventTrans.TransRefId
           && salesTable.Extraordinary  == NoYes::Yes;

The sql generated nests the second notExists join (salesTable) into the where clause of the first notExists join (custTable). ie

SELECT SUM(A.QTY)
FROM
    INVENTTRANS A
WHERE
    A.DATAAREAID  = 'MyCompany'
AND A.ITEMID      = 'MyItem'
AND A.TRANSTYPE   = 0
AND A.INVENTDIMID = 'SomeValue'
AND NOT EXISTS (SELECT 'x'
                FROM CUSTTABLE C
                WHERE C.DATAAREAID  ='MyCompany'
                AND C.ACCOUNTNUM    =A.CUSTVENDAC
                AND C.CUSTGROUP     ='SomeCustGroup'
                AND NOT EXISTS (SELECT 'x'
                                FROM SALESTABLE B
                                WHERE
                                    B.DATAAREAID    ='MyCompany'
                                AND B.SALESID       =A.TRANSREFID
                                AND B.EXTRAORDINARY =1))

Is there any way to write the X++ select query to notExists join the salesTable to the inventTrans table instead of the custTable, so the SQL generated would be similar to the following?

SELECT SUM(A.QTY)
FROM
    INVENTTRANS A
WHERE A.DATAAREAID     = 'MyCompany'
AND A.ITEMID           = 'MyItem'
AND A.TRANSTYPE        = 0
AND A.INVENTDIMID      = 'SomeValue'
AND NOT EXISTS (SELECT 'x'
                FROM CUSTTABLE C
                WHERE C.DATAAREAID  ='MyCompany'
                AND C.ACCOUNTNUM    =A.CUSTVENDAC
                AND C.CUSTGROUP     ='SomeCustGroup')
AND NOT EXISTS (SELECT 'x'
                FROM SALESTABLE B
                WHERE
                    B.DATAAREAID='MyCompany'
                AND B.SALESID=A.TRANSREFID
                AND B.EXTRAORDINARY=1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-07-20 17:15:12

简短的回答:不!

在您的示例中,您可以使用内部联接重新制定:

select sum(qty) from inventTrans
    index hint TransTypeIdx
    where inventTrans.ItemId        == itemId
       && inventTrans.TransType     == InventTransType::Sales
       && inventTrans.InventDimId   == inventDimId
    join salesTable
    where salesTable.SalesId        == inventTrans.TransRefId
       && salesTable.Extraordinary  == NoYes::No;
    notExists join custTable
    where custTable.AccountNum      == inventTrans.CustVendAC
       && custTable.CustGroup       == custGroupId

只要您在开票时不删除销售订单,它应该可以工作。

The short answer: no!

In your example, you could reformulate using inner join:

select sum(qty) from inventTrans
    index hint TransTypeIdx
    where inventTrans.ItemId        == itemId
       && inventTrans.TransType     == InventTransType::Sales
       && inventTrans.InventDimId   == inventDimId
    join salesTable
    where salesTable.SalesId        == inventTrans.TransRefId
       && salesTable.Extraordinary  == NoYes::No;
    notExists join custTable
    where custTable.AccountNum      == inventTrans.CustVendAC
       && custTable.CustGroup       == custGroupId

It should work provided you do not delete sales orders when invoicing.

泪意 2024-07-20 17:15:12

您可能不再等待答案,但在 Axe 2012 上我仍然遇到了同样的问题。

找到了另一种解决方案(除了使用 SQL 语句之外),不是性能最高的解决方案,但它有效:

  • 创建查询: select * from inventTrans noteexists join CustTable ....
  • 基于该查询创建一个视图
  • 对 salesTable 执行相同操作
  • 按如下方式更改您的选择语句:

    选择 inventTrans

    ...

    存在加入 myCustView

    其中 myCustView.InventTransRecId == inventTrans.RecId

    存在加入 mySalesView

    where ...

希望这有帮助,

T

You're probably not waiting for an answer anymore, but on Ax 2012 I still experienced the same problem.

Found another solution (aside from using a SQL Statement), not the most performant one, but it works:

  • Create a query: select * from inventTrans notexists join CustTable ....
  • Create a view based on that query
  • do the same for the salesTable
  • change your select statement as follows:

    select inventTrans

    ...

    exists join myCustView

    where myCustView.InventTransRecId == inventTrans.RecId

    exists join mySalesView

    where ...

Hope this helps,

T

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文