【问题标题】:Can min() be modified to select the minumum value that is above 0可以修改min()来选择大于0的最小值吗
【发布时间】:2022-01-10 13:53:52
【问题描述】:

我有一个 sql 表,我想选择不包括 0 的最小值。知道该怎么做吗?

【问题讨论】:

  • 一个 where 条件可以解决这个问题,而不是尝试修复 MIN 函数
  • 能否请您提供一些示例数据和所需的输出。现在看起来你可以使用 SELECT MIN(COLUMN)FROM YOUR_TABLE WHERE COLUMN 0
  • minumum value that is above 0min value excluding 0 是非常不同的东西。你想要什么?
  • 在标准 SQL 中,您可以使用 min(the_column) filter (where the_column > 0)
  • 您使用的是哪种 DBMS 产品? “SQL”只是所有关系数据库都使用的一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加tagWhy should I tag my DBMS

标签: sql min


【解决方案1】:

可以执行以下请求

SELECT min(field)
FROM table
WHERE field != 0

【讨论】:

  • 感谢您的回答。我已经尝试添加 where 条件,但它不起作用。这是我写的代码: declare @min int = (select min(note) from dbo.notation where min(note) != 0); print cast(@min as varchar);我还尝试了 > 0 条件。
  • 我用 SQlite 试过了,效果很好,你用的是哪个 RDBMS?
  • Azure 数据工作室
  • 哎呀,帮不了你:)
【解决方案2】:

您提供的查询(如下所示)将不起作用,因为您在 where 子句中使用了聚合函数 (min),这是不允许的。

declare @min int = (select min(note) from dbo.notation **where min(note) != 0)**; print cast(@min as varchar);

我认为 Maxime Challon 的查询会给您预期的结果。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2019-05-09
  • 2020-07-04
  • 1970-01-01
  • 2010-11-10
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2016-06-21
相关资源
最近更新 更多