2017-03-22 40 views
2

有没有机会加入这样的两张桌子?while选择加入firstOnly

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没有成功...所以这里我要求一个想法。

回答

0

我不得不承认,这是迄今为止我已经写过的最奇怪的查询:

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 

的几个注意事项:

。取而代之的

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 

党从这里开始,从我所看到的,使用一组由一次,全部来自其它表中的字段似乎是空的。所以解决的办法是到到处添加组。

你看我加入到RecId分组,以确保我不是真的分组任何:)

为了得到你所拥有的SELECT子句中添加聚合函数值的字段。好的,很酷,为什么不呢,只要我不是真正的分组。

。但抓我是在最后一部分:

join maxof(QuotationDate) from custQuotationJour 
    order by custQuotationJour.QuotationId 
      where custQuotationJour.QuotationId == custQuotationSalesLink.QuotationId 
       && custQuotationJour.QuotationDate == custQuotationSalesLink.QuotationDate 

为了通过的伎俩。我不知道为什么。如果我用组切换它这对我来说很正常,我得到重复的值。所以之前添加的所有分组都失去了相关性。我想有时候也有一点运气可以加入比赛。我的意思是,为什么在订购那里。也许是因为周三,我不知道。

我不得不在最后的连接上使用一些聚合,否则我不会得到QuotationDate字段的值,实际上这是该工作的整个目标。

此链接对我帮助很大:

http://axatluegisdorf.blogspot.ca/2010/07/select-group-by-and-join-order-by.html

1

你不能在一个select语句中做到这一点。 首先,您可以在SalesId上创建一个视图,并在您需要的字段上分组SalesOd和maxOf,minOf ...。 此视图应为每个SalesId只返回一条记录。您可以将此视图加入销售表。

如果你只想得到第一行的命令,那么你必须做嵌套选择。 最好的方法是创建一个包含所需字段的临时表并将其填充数据。

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语句 - 唯一的),这将很好地工作

select firstOnly SalesPrice, ItemId, LineNum from salesLine 
    order by salesLine.LineDisc asc, salesLine.SalesPrice desc 
    where salesLine.SalesId == "xxx006932683"; 
+0

你怎么能确保'LineDisc'和'SalesPrice'顺序,并包括'SalesPrice','ItemId'和'LineNum'在使用这种方法的字段列表? –