【问题标题】:SuiteQL cannot includes keyword 'DISTINCT'?SuiteQL 不能包含关键字“DISTINCT”?
【发布时间】:2020-12-20 18:08:25
【问题描述】:

当我尝试在 SuiteQL 中使用 distinct 关键字时,我不断收到错误消息。

这是我的代码:

var bannedItemIdArr = [];
var querySQL = "SELECT DISTINCT item.ID AS idRAW /*{id#RAW}*/ FROM item, (SELECT itemMember.parentitem AS parentitem, itemMember.parentitem AS parentitem_join, itemMember.ID AS ID, item_0.itemtype AS itemtype, itemMember.item AS item, item_0.itemtype AS itemtype_crit, item_0.isinactive AS isinactive_crit FROM itemMember, item item_0 WHERE itemMember.item = item_0.ID(+)) itemMember_SUB WHERE item.ID = itemMember_SUB.parentitem(+) AND ((UPPER(item.itemtype) IN ('ASSEMBLY') AND UPPER(itemMember_SUB.itemtype_crit) IN ('INVTPART', 'ASSEMBLY') AND itemMember_SUB.isinactive_crit = 'T' AND NVL(item.isinactive, 'F') = 'F'))";
var myPagedResults = query.runSuiteQLPaged({ query: querySQL, pageSize: 1000, });

错误:

发生搜索错误:搜索无效或不受支持

我该如何解决这个错误?

【问题讨论】:

  • 请重新打开这个问题:它包含足够的细节来回答。
  • 其他细节:调用 runSuiteQLPaged 时效果很好,但是当我从结果中获取数据时出错。 myPagedResults.fetch({index: 0});//导致错误行
  • 我相信这个错误已经随风而逝了,因为 SuiteQL 现在支持关键字“Distinct”。您可以在 chrome 控制台或timdietrich's SuiteQL 工具中尝试使用“SELECT DISTINCT Status FROM Transaction”进行测试;虽然我仍然不知道哪个更新修复了这样的问题。

标签: sql netsuite suitescript


【解决方案1】:

你看过 query.Aggregate 吗?

Enum Description Module
Holds the string values for aggregate functions supported with the N/query Module. An aggregate function performs a calculation on the column or condition values and returns a single value. Each value in this enum (except MEDIAN) has two variants: distinct (using the _DISTINCT suffix) and nondistinct (using no suffix). The variant determines whether the aggregate function operates on all instances of duplicate values or on just a single instance of the value. For example, consider a situation in which the MAXIMUM aggregate function is used to determine the maximum of a set of values. When using the distinct variant (MAXIMUM_DISTINCT), the aggregate function considers each instance of duplicate values. So if the set of values includes three distinct values that are all equal and all represent the maximum value in the set, the aggregate function lists all three instances. When using the nondistinct variant (MAXIMUM), only one instance of the maximum value is listed, regardless of the number of instances of that maximum value in the set. This enum is used to pass the aggregate function argument to Component.createColumn(options), Component.createCondition(options), Query.createColumn(options), and Query.createCondition(options). N/query Module

价值观

Value Description
AVERAGE Calculates the average value.
AVERAGE_DISTINCT Calculates the average distinct value.
COUNT Counts the number of results.
COUNT_DISTINCT Counts the number of distinct results.
MAXIMUM Determines the maximum value. If the values are dates, the most recent date is determined.
MAXIMUM_DISTINCT Determines the maximum distinct value. If the values are dates, the most recent date is determined.
MEDIAN Calculates the median value
MINIMUM Determines the minimum value. If the values are dates, the earliest date is determined.
MINIMUM_DISTINCT Determines the minimum distinct value. If the values are dates, the earliest date is determined.
SUM Adds all values.
SUM_DISTINCT Adds all distinct values.

语法

// Add additional code
...
var myTransactionQuery = query.create({
    type: query.Type.TRANSACTION
});

var myAggColumn = myTransactionQuery.createColumn({
    fieldId: 'amount',
    aggregate: query.Aggregate.AVERAGE
});

myTransactionQuery.columns = [myAggColumn];
...
// Add additional code

【讨论】:

  • 感谢您的提示,但我认为我没有正确描述我的情况,所以我只是为我的问题添加更多细节。我只想在我的 SuiteQL 中使用关键字“DISTINCT”,然后获取 myPagedResults。如果我不使用 Paged api,不要获取数据,一切顺利。我不确定“获取”理论。
【解决方案2】:

所以这里的诀窍是使用group by 而不是distinct

例如,

select item.id
from item
group by item.id

也就是说,您的查询看起来像是使用旧的特定于 oracle 的连接语法(使用 (+))而不是套件 ql 支持的 left joinright join 的 ANSI92 语法。此外,您必须更改 nvl 以使用 ANSI92 coalesce

【讨论】:

  • 感谢您的提示。它在用“group by”替换“distinct”时起作用。至于其他的语法差异,我下次会注意到。
  • 有没有办法从itemGROUP BY item.id 中选择所有列?当我尝试SELECT item.id, item.* FROM item GROUP BY item.id 时,SuiteQL 抱怨“发生搜索错误:搜索无效或不受支持”。
  • 为了做到这一点,您必须像在 pl/sql 或 t/sql 中一样列出未在组中聚合的每个字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 2018-08-14
相关资源
最近更新 更多