我不得不承认,这是迄今为止我已经写过的最奇怪的查询:
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'在使用这种方法的字段列表? –