我不得不承認,這是迄今爲止我已經寫過的最奇怪的查詢:
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
你怎麼能確保'LineDisc'和'SalesPrice'順序,幷包括'SalesPrice','ItemId'和'LineNum'在使用這種方法的字段列表? –