Coldfusion 具有空值的查询列上的ArrayMin

Coldfusion 具有空值的查询列上的ArrayMin,coldfusion,Coldfusion,CF8 我使用这一行来获取查询列的最小值。我刚刚注意到记录集中的空值会导致错误。有没有简单的方法告诉ArrayMin跳过空值,而不必循环列并加载一个包含所有非空值的数组 <cfset temp_data_min = #round(ArrayMin(query_x["some_field"]))#> 谢谢 最简单的方法可能是只使用该列进行查询并删除空值 <cfquery name="query_x_fixed" dbtype="query"> SELECT some_fi

CF8

我使用这一行来获取查询列的最小值。我刚刚注意到记录集中的空值会导致错误。有没有简单的方法告诉ArrayMin跳过空值,而不必循环列并加载一个包含所有非空值的数组

<cfset temp_data_min = #round(ArrayMin(query_x["some_field"]))#>

谢谢

最简单的方法可能是只使用该列进行查询并删除空值

<cfquery name="query_x_fixed" dbtype="query">
SELECT some_field
FROM query_x
WHERE some_field IS NOT NULL
</cfquery>

<cfset temp_data_min= round(ArrayMin(query_x_fixed["some_field"]))>

未测试

最简单的方法可能是仅使用该列进行查询并删除空值

<cfquery name="query_x_fixed" dbtype="query">
SELECT some_field
FROM query_x
WHERE some_field IS NOT NULL
</cfquery>

<cfset temp_data_min= round(ArrayMin(query_x_fixed["some_field"]))>

未测试

您可以在数组中循环并创建一个不包含任何空值的新数组。然后将ArrayMin函数应用于新数组

<cfset newArray = []>
<cfloop index="x" array="#query_x["some_field"]#">
  <cfif x NEQ 'null'>
      <cfset arrayAppend(newArray, x)>
  </cfif>
</cfloop>
<cfset temp_data_min = round(ArrayMin(newArray))>

未测试

您可以在数组中循环并创建一个不包含任何空值的新数组。然后将ArrayMin函数应用于新数组

<cfset newArray = []>
<cfloop index="x" array="#query_x["some_field"]#">
  <cfif x NEQ 'null'>
      <cfset arrayAppend(newArray, x)>
  </cfif>
</cfloop>
<cfset temp_data_min = round(ArrayMin(newArray))>

未测试

基于Al使用查询查询所说的内容,只是将Min调用添加到查询中

<cfquery name="query_x_fixed" dbtype="query">
SELECT Min(some_field) as some_field
FROM query_x
WHERE some_field IS NOT NULL
</cfquery>

<cfset temp_data_min = round(query_x_fixed.some_field)>

测试在CF9中工作

基于Al使用查询查询所说的内容,只需将Min调用添加到查询中

<cfquery name="query_x_fixed" dbtype="query">
SELECT Min(some_field) as some_field
FROM query_x
WHERE some_field IS NOT NULL
</cfquery>

<cfset temp_data_min = round(query_x_fixed.some_field)>

经过测试,可以在CF9中工作。通过将列转换为列表,然后转换为数组,可以将解决方案保留在一行。ListToArray默认为忽略空列表项,这就是空值

<cfset temp_data_min = Round(ArrayMin(ListToArray(ValueList(query_x.some_field, Chr(7)), Chr(7))))>
这应该比任何其他建议的解决方案都要快,而且代码更少

我已经为使用逗号作为数字小数分隔符的区域设置指定了分隔符。如果您在美国或其他使用。然后可以删除分隔符参数

使用的其他功能:


通过将列转换为列表,然后转换为数组,可以将解决方案保留在一行。ListToArray默认为忽略空列表项,这就是空值

<cfset temp_data_min = Round(ArrayMin(ListToArray(ValueList(query_x.some_field, Chr(7)), Chr(7))))>
这应该比任何其他建议的解决方案都要快,而且代码更少

我已经为使用逗号作为数字小数分隔符的区域设置指定了分隔符。如果您在美国或其他使用。然后可以删除分隔符参数

使用的其他功能:


顺便问一下:你的标记完全不必要。只是为了检查:你有没有理由不能从原始查询中排除null?顺便问一下:你的标记完全不必要。只是为了检查:你有没有理由不能从原始查询中排除null?谢谢-这很好。没有更好的办法了。非常感谢!谢谢-很好用。没有更好的办法了。非常感谢!