Axapta 而仅使用join FIRSTNONLY选择

Axapta 而仅使用join FIRSTNONLY选择,axapta,microsoft-dynamics,x++,dynamics-ax-2009,Axapta,Microsoft Dynamics,X++,Dynamics Ax 2009,我有没有可能像这样加入两张桌子 while select SalesId from salesTable //group by SalesId where salesTable.SalesId == "xxx006932683" join firstOnly SalesPrice, ItemId, LineNum from salesLine //group by SalesId order by salesLine.LineDisc asc, sale

我有没有可能像这样加入两张桌子

while select  SalesId from salesTable
    //group by SalesId
    where salesTable.SalesId == "xxx006932683"
    join firstOnly SalesPrice, ItemId, LineNum from salesLine
    //group by SalesId
    order by salesLine.LineDisc asc, salesLine.SalesPrice desc 
    where salesLine.SalesId == salesTable.SalesId
{
    info(strFmt("Sales id : %1 line %2 item %3 price %4", salesLine.SalesId, salesLine.LineNum, salesLine.ItemId, salesLine.SalesPrice));
}
因此,对于
SalesTable
中的每一行,只将其与
SalesLine
中的一行连接,该行具有相同的
SalesId
,并满足订单条件


老实说,我尝试过很多分组和排序,以及maxOfs和minOfs,但都没有成功。。。因此,我在这里要求一个想法

不能在一个select语句中执行此操作。 首先,您可以在sales line上创建一个视图,并在SalesId和maxOf、minOf、。。。在你需要的领域。 对于每个SalesId,此视图应该只返回一条记录。然后,您可以将此视图连接到sales表

如果您只想获得订单的第一行,那么您必须进行嵌套选择。 最好的方法是创建一个包含所需字段的临时表,并用数据填充它

while select SalesId from salesTable
{
   select firstOnly SalesPrice, ItemId, LineNum from salesLine
      order by salesLine.LineDisc asc, salesLine.SalesPrice desc 
      where salesLine.SalesId == salesTable.SalesId
   ;

   //insert into temp table

   info(strFmt("Sales id : %1 line %2 item %3 price %4", salesLine.SalesId, salesLine.LineNum, salesLine.ItemId, salesLine.SalesPrice));
}

但是在你的情况下(因为你在SalesId上有where语句),我不得不承认这是我迄今为止写过的最奇怪的查询:

SalesQuotationTable salesQuotationTable;
SalesQuotationLine  salesQuotationLine;

CustQuotationSalesLink    custQuotationSalesLink;
CustQuotationJour         custQuotationJour;
;  

while select maxof(QuotationId), maxof(CurrencyCode) from salesQuotationTable
    group by QuotationId, CurrencyCode, RecId
         where salesQuotationTable.QuotationStatus    == SalesQuotationStatus::Sent
            && salesQuotationTable.QuotationType      == QuotationType::Sales
          //&& salesQuotationTable.QuotationId        == '00015683_042' just for testing

join maxof(lineNum), minof(lineAmount), maxof(QuotationId) from salesQuotationLine
    group by lineNum, lineAmount, QuotationId, RecId
         where salesQuotationLine.QuotationId         == salesQuotationTable.QuotationId
            && salesQuotationLine.QuotationStatus     == SalesQuotationStatus::Sent
            && salesQuotationLine.QuotationType       == QuotationType::Sales
            && salesQuotationLine.SalesQty            > 0

//duplicate values were coming from here, grouping was the way to go
join maxof(QuotationDate), maxof(QuotationId) from custQuotationSalesLink
    group by OrigQuotationId
         where custQuotationSalesLink.OrigQuotationId == salesQuotationTable.QuotationId

join maxof(QuotationDate) from custQuotationJour
         order by custQuotationJour.QuotationId
             where custQuotationJour.QuotationId      == custQuotationSalesLink.QuotationId
                && custQuotationJour.QuotationDate    == custQuotationSalesLink.QuotationDate
请注意:

1。而不是

select firstonly custQuotationSalesLink
     order by QuotationDate desc, QuotationId desc
        where custQuotationSalesLink.OrigQuotationId == this.QuotationId
我用过

join maxof(QuotationDate), maxof(QuotationId) from custQuotationSalesLink
     group by OrigQuotationId
         where custQuotationSalesLink.OrigQuotationId == salesQuotationTable.QuotationId
聚会从这里开始,据我所见,一旦使用group by,其他表中的所有字段似乎都是空的。因此解决方案是在所有地方添加group by。

您可以看到,我正在将RecId添加到分组中,以确保我没有真正分组任何内容:)

为了获得具有值的字段,必须在select子句中添加聚合函数。好吧,酷,为什么不呢,只要我不是真的分组

2。但对我来说,最后一个问题是:

join maxof(QuotationDate) from custQuotationJour
    order by custQuotationJour.QuotationId
          where custQuotationJour.QuotationId == custQuotationSalesLink.QuotationId
                 && custQuotationJour.QuotationDate == custQuotationSalesLink.QuotationDate
按订购的成功了。我不知道为什么。如果我用一个对我来说很正常的groupby来切换它,我会得到重复的值。所以之前添加的所有分组都失去了相关性。我想有时候也需要一点运气。我是说,为什么要按顺序思考呢。也许是因为今天是星期三,我不知道

我必须在最后一次连接上使用一些聚合,因为否则我就不会得到
QuotationDate
字段的值,这实际上是这项工作的全部目标

这个链接帮了我很多忙:


如何确保通过
LineDisc
saleprice
进行订购,并使用此方法将
saleprice
ItemId
LineNum
包含在字段列表中?
join maxof(QuotationDate) from custQuotationJour
    order by custQuotationJour.QuotationId
          where custQuotationJour.QuotationId == custQuotationSalesLink.QuotationId
                 && custQuotationJour.QuotationDate == custQuotationSalesLink.QuotationDate