postgresql - 将 View 加入另一个表

标签 postgresql view

是否可以在 SQL 中将一个 View 与另一个表连接起来?如果是,怎么办?

我对具有特定字段的 Oracle 数据库有一个查询。我需要在 PostgreSQL 上重新创建相同的查询,但 PostgreSQL 查询中的一些数据来自一个 View ......并且该 View 缺少信息。这是一个非常复杂的 View ,所以我暂时不想使用它。

例如,在 Oracle 中我这样做:

SELECT
d.dos_id,
trunc(d.dos_creation, 'MM') as Cohorte,
sum(v.ver_etude + v.ver_direct) as encaissé
from t_dossier d
left outer join v_versement v
on v.dos_id = d.dos_id

在 Postgres 中,我使用的是 View 。但是 View 不返回“dos_id”,所以我不能明确地将 v_versement 加入 View 。

有没有办法强制 View 在运行时返回创建 View 时不存在的特定字段?

最佳答案

你不能强制它

to return specific fields at runtime which weren't there when creating the view

您可以通过限制创建或替换它:

https://www.postgresql.org/docs/current/static/sql-createview.html

CREATE OR REPLACE VIEW is similar, but if a view of the same name already exists, it is replaced. The new query must generate the same columns that were generated by the existing view query (that is, the same column names in the same order and with the same data types), but it may add additional columns to the end of the list. The calculations giving rise to the output columns may be completely different.

例子:

t=# create view v2 as select now();
CREATE VIEW
Time: 36.488 ms
t=# create or replace view v2 as select now(),current_user;
CREATE VIEW
Time: 8.551 ms
t=# create or replace view v2 as select now()::text,current_user;
ERROR:  cannot change data type of view column "now" from timestamp with time zone to text
Time: 0.430 ms

关于postgresql - 将 View 加入另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44175496/

相关文章:

php - 对于每个/查询没有给出正确的值

macos - 如何在 Swift 中删除一个 View 的所有 subview ?

java - MVC - 如何将数据从 View 传递到 Controller

c# - SQL View 上的 LINQ 选择得到错误的答案

ios - 带有渐变边缘的按钮下方的 BlurView

sql - 计数为零的项目列表不存在

sql - Postgres 条件选择?

database - Postgresql - 如果记录不存在则以干净的方式插入记录,如果存在则更新

sql - 在第一次引导事件发生之前查找平均观看次数

android - 如何查看我的 Android 应用程序数据库信息?