where 子句中的 BQL 子查询

BQL subquery in the where clause

您好,我在一些 BQL 语法上遇到了问题,我想要实现的是如下所示的 BQL 语句 SQL,在 where 子句中带有嵌套子查询。

  SELECT * FROM ARInvoice I 
WHERE (SELECT COUNT(*) FROM ARAdjust A WHERE I.RefNbr = A.AdjdRefNbr) > 0

这在 BQL 中是否可行,如果可以,我将如何编写?

下面是我目前得到的,但这不正确我遇到了语法错误

   PXSelect<PX.Objects.AR.ARInvoice,
        Where<PXSelectGroupBy<PX.Objects.AR.ARAdjust, Where<PX.Objects.AR.ARAdjust.adjdRefNbr, Equal<PX.Objects.AR.ARInvoice.refNbr>, Aggregate<Count>>, Greater<Zero>>>>.Select(new PXGraph());

谢谢

您有两种选择来实现它。

使用子查询:

您可以在 ARInvoice 中添加一个未绑定的计算字段 (PXDBScalar)

要在 BQL 中添加子查询,您必须在属性级别进行。因为你想查询另一个table,PXDBScalar,将是最好的选择。如果你想查询同一条记录的其他字段,PXDBCalced 就足够了。有关 Advanced SQL Attributes 的更多信息,请参阅 Using Advanced SQL Attributes 下的 T200 和 Help -> Acumatica Framework -> API Reference -> Attributes -> Adhoc SQL 对于字段。

扩展 ARInvoice (V5.1 及以下版本)

public class ARInvoiceExtension : PXCacheExtension<ARInvoice>
{
    public abstract class lastPaymentOrderNbr : IBqlField
    {
    }

    #region LastPaymentOrderNbr
    [PXString]
    [PXUIField(DisplayName = "Last Payment Order Nbr.")]
    [PXDBScalar(typeof(Search<ARAdjust.adjdOrderNbr,
                                 Where<ARAdjust.adjdDocType, Equal<ARInvoice.docType>,
                                    And<ARAdjust.adjdRefNbr, Equal<ARInvoice.refNbr>>>,
                                    OrderBy<Desc<ARAdjust.adjgDocDate>>>))]
    public string LastPaymentOrderNbr { get; set; }
    #endregion
}

V5.2 中的 ARInvoice 中添加了一个新字段以获取最后付款日期,因此您不必添加另一个字段:

public abstract class lastPaymentDate : PX.Data.IBqlField
{
}
protected DateTime? _LastPaymentDate;

/// <summary>
/// The date of the most recent payment associated with this document.
/// </summary>
[PXDate()]
[PXDBScalar(typeof(Search<ARAdjust.adjgDocDate,
 Where<ARAdjust.adjdDocType, Equal<ARInvoice.docType>,
    And<ARAdjust.adjdRefNbr , Equal<ARInvoice.refNbr>>>,
    OrderBy<Desc<ARAdjust.adjgDocDate>>>))]
[PXUIField(DisplayName = "Last Payment Date")]
public virtual DateTime? LastPaymentDate
{
    get
    {
        return this._LastPaymentDate;
    }
    set
    {
        this._LastPaymentDate = value;
    }
}

您的 PXSelect 将如下所示:

V5.1及以下

public PXSelect<ARInvoice, Where<ARInvoiceExtension.lastPaymentOrderNbr, IsNotNull>> InvoicesTest;

V5.2

public PXSelect<ARInvoice, Where<ARInvoice.lastPaymentDate, IsNotNull>> InvoicesTest;

内部联接 table

您可以简单地添加一个没有 ARAdjust 的内部连接和过滤记录,而不是对其进行子查询。然后按关键字段分组以避免重复。

        public PXSelectJoinGroupBy<ARInvoice, 
                InnerJoin<ARAdjust, On<ARAdjust.adjdRefNbr, Equal<ARInvoice.refNbr>, 
                    And<ARAdjust.adjdDocType, Equal<ARInvoice.docType>>>>, 
                Where<ARAdjust.adjdOrderNbr, IsNotNull>, 
                Aggregate<GroupBy<ARInvoice.docType, 
                    GroupBy<ARInvoice.refNbr>>>> InvoicesTest;