postgresql - Virtual Column Only Executing for First Row
问题描述
I'm trying to used a function to create a virtual column on my table. The function executes correctly on its own, but when I add to my table:
SELECT *, table_a.function FROM table_a;
It only executes the function for the first row of the result and gives the same output for all subsequent rows.
Example Function:
CREATE FUNCTION overnight(table_a)
RETURNS boolean AS
$$
SELECT CASE WHEN (((duration / 60) * 100) + (duration % 60) + departure) >= 2400 THEN TRUE ELSE FALSE END FROM schema.table_a
$$;
How do I ensure the code executes for every row of my table?
Thanks!
解决方案
让您的函数接受您需要的字段,而不是整个表。
SELECT *, overnight(duration,departure) FROM table_a;
CREATE FUNCTION overnight(duration,departure)
RETURNS boolean AS
SELECT CASE WHEN ... THEN TRUE ELSE FALSE END;
$$;
推荐阅读
- codeblocks - Code::Blocks 突然停止工作并且不再打开
- asp.net-mvc - Mac VS2019 ASP.net MVC 上的 Razor 页面无法使用 _Layout.cshtml 正确呈现
- python - 如何构建 python / Flask 项目
- python - 比较两个列表的元素 0;使用 z[0] 中不在 x 中的项目创建新列表列表
- azure - Azure 计时器函数应用异常处理
- python - Groupby 有 2 列 - “pandas.core.groupby.generic.DataFrameGroupBy”终端响应
- python - 如何计算一列列表中字符串单词的数量?
- elliptic-curve - ECIES:KDF的正确ECDH输入方式?安全效果?
- javascript - sendemail 的 Google AppScript 语法错误。无法识别我的问题
- python - 无法从 django 中的 firebase 检索数据