英文:
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
只能在SELECT
和FROM
子句中指定别名,而不能在WHERE
中指定。但也许您的问题是如何在WHERE
条件中使用在SELECT
列表中定义的别名。这是不允许的,因为WHERE
在SELECT
之前执行。但您可以使用子查询:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论