在查找最小值时出现的别名问题

huangapple go评论53阅读模式
英文:

Alias issue when find minimum

问题

I have one query:

Partial:

and (("Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" >= (select "threshold1" from "T_Everything" where "Frame" = 1 ))

I Like to create one Alias only from this partial.

"Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" as difference

So I can use later the difference. But I tried many ways and I get syntax error with the "as" command. Try to put between () also not work.
Anything helps.

Thank you.

英文:

I have one query:

Partial:

and (("Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" >= (select "threshold1" from "T_Everything" where "Frame" = 1 ))

I Like to create one Alias only from this partial.

"Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" as difference

So I can use later the difference. But I tried many ways and I get syntax eror with the "as" command. Try to put between () also not work.
Anything helps.

Thank you.

答案1

得分: 1

只能在SELECTFROM子句中指定别名,而不能在WHERE中指定。但也许您的问题是如何在WHERE条件中使用在SELECT列表中定义的别名。这是不允许的,因为WHERESELECT之前执行。但您可以使用子查询:

SELECT somecolumn, difference
FROM (SELECT somecolumn,
             "Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" AS difference
      FROM tab) AS table_alias
WHERE table_alias.difference >= 41;

尽管看起来有点复杂,但PostgreSQL会“提升”子查询,查询性能可以很好。

英文:

You can only specify an alias in the SELECT and FROM clauses, not in WHERE. But perhaps your problem is how to use an alias defined in the SELECT list in a WHERE condition. That is not allowed, because WHERE comes before SELECT. But you can use a subquery:

SELECT somecolumn, difference
FROM (SELECT somecolumn,
             "Table_Status"."Status_Lenght" - "Table_Status"."Min_Lenght" AS difference
      FROM tab) AS table_alias
WHERE table_alias.difference >= 41;

Even though that looks complicated, PostgreSQL will “pull up” the subquery, and the query can perform well.

huangapple
  • 本文由 发表于 2023年2月27日 19:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定