2017-03-22 41 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'在使用這種方法的字段列表? –