while select with join firstOnly
while select with join 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 都没有成功...所以我在这里寻求一个想法。
您不能在一个 select 语句中做到这一点。
首先,您可以在销售线上创建一个视图,并根据您需要的字段对 SalesId 和 maxOf、minOf 等进行分组。
此视图应 return 每个 SalesId 仅一个记录。您可以将此视图加入销售 table。
如果您只想获得第一行订单,那么您必须嵌套 selects。
最好的方法是用你需要的字段创建一个临时 table 并用数据填充它。
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 <- unique 上有 where 语句)这会很好
select firstOnly SalesPrice, ItemId, LineNum from salesLine
order by salesLine.LineDisc asc, salesLine.SalesPrice desc
where salesLine.SalesId == "xxx006932683";
我不得不承认这是迄今为止我写过的最奇怪的查询:
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
派对从这里开始,据我所见,一旦使用分组依据,其他表中的所有字段似乎都是空的。所以解决方法是到处添加分组。
你看我正在将 RecId 添加到分组中以确保我没有真正分组任何东西:)
为了获取具有值的字段,您必须在 select 子句中添加聚合函数。好的,很好,为什么不呢,只要我不是真正的分组。
2。但我的收获是在最后一部分:
join maxof(QuotationDate) from custQuotationJour
order by custQuotationJour.QuotationId
where custQuotationJour.QuotationId == custQuotationSalesLink.QuotationId
&& custQuotationJour.QuotationDate == custQuotationSalesLink.QuotationDate
order by 成功了。我不知道为什么。如果我用 group by 切换它,这对我来说很正常,我会得到重复的值。所以之前添加的所有分组都失去了相关性。我想有时也需要一点运气才能加入游戏。我的意思是,为什么要考虑 order by 那里。也许因为是星期三,我不知道。
我不得不在最后一个连接上使用一些聚合,否则我不会得到 QuotationDate
字段的值,这实际上是这项工作的全部目标。
这个link对我帮助很大:
http://axatluegisdorf.blogspot.ca/2010/07/select-group-by-and-join-order-by.html
我有机会像这样连接两张桌子吗?
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 语句中做到这一点。 首先,您可以在销售线上创建一个视图,并根据您需要的字段对 SalesId 和 maxOf、minOf 等进行分组。 此视图应 return 每个 SalesId 仅一个记录。您可以将此视图加入销售 table。
如果您只想获得第一行订单,那么您必须嵌套 selects。 最好的方法是用你需要的字段创建一个临时 table 并用数据填充它。
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 <- unique 上有 where 语句)这会很好
select firstOnly SalesPrice, ItemId, LineNum from salesLine
order by salesLine.LineDisc asc, salesLine.SalesPrice desc
where salesLine.SalesId == "xxx006932683";
我不得不承认这是迄今为止我写过的最奇怪的查询:
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
派对从这里开始,据我所见,一旦使用分组依据,其他表中的所有字段似乎都是空的。所以解决方法是到处添加分组。
你看我正在将 RecId 添加到分组中以确保我没有真正分组任何东西:)
为了获取具有值的字段,您必须在 select 子句中添加聚合函数。好的,很好,为什么不呢,只要我不是真正的分组。
2。但我的收获是在最后一部分:
join maxof(QuotationDate) from custQuotationJour
order by custQuotationJour.QuotationId
where custQuotationJour.QuotationId == custQuotationSalesLink.QuotationId
&& custQuotationJour.QuotationDate == custQuotationSalesLink.QuotationDate
order by 成功了。我不知道为什么。如果我用 group by 切换它,这对我来说很正常,我会得到重复的值。所以之前添加的所有分组都失去了相关性。我想有时也需要一点运气才能加入游戏。我的意思是,为什么要考虑 order by 那里。也许因为是星期三,我不知道。
我不得不在最后一个连接上使用一些聚合,否则我不会得到 QuotationDate
字段的值,这实际上是这项工作的全部目标。
这个link对我帮助很大:
http://axatluegisdorf.blogspot.ca/2010/07/select-group-by-and-join-order-by.html