在源目标表中生成访问矩阵
我在PostgreSQL中有两个表
shasonitance
中的每一行都有2个类ID source_id
和 target_id
:
CREATE TABLE public.class (
id bigint NOT NULL DEFAULT nextval('class_id_seq'::regclass),
name character varying(500) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT class_pkey PRIMARY KEY (id)
)
CREATE TABLE public.inheritance (
id bigint NOT NULL DEFAULT nextval('inherited_id_seq'::regclass),
source_id bigint NOT NULL,
target_id bigint NOT NULL,
CONSTRAINT inherited_pkey PRIMARY KEY (id),
CONSTRAINT inherited_source_id_fkey FOREIGN KEY (source_id)
REFERENCES public.class (id),
CONSTRAINT inherited_target_id_fkey FOREIGN KEY (target_id)
REFERENCES public.class (id)
)
我想在基于继承表中的继承关系中的所有类之间创建访问矩阵。 我尝试此代码:
select * , case when id in (select target_id from inheritance where source_id=1) then 1 else 0 end as "1"
, case when id in (select target_id from inheritance where source_id=2) then 1 else 0 end as "2"
, case when id in (select target_id from inheritance where source_id=3) then 1 else 0 end as "3"
, case when id in (select target_id from inheritance where source_id=4) then 1 else 0 end as "4"
, case when id in (select target_id from inheritance where source_id=5) then 1 else 0 end as "5"
, case when id in (select target_id from inheritance where source_id=6) then 1 else 0 end as "6"
, case when id in (select target_id from inheritance where source_id=7) then 1 else 0 end as "7"
, case when id in (select target_id from inheritance where source_id=8) then 1 else 0 end as "8"
, case when id in (select target_id from inheritance where source_id=9) then 1 else 0 end as "9"
from class
并获得正确的答案,但仅适用于课堂上的9个静态行。
如何使用Dynamic SQL命令在类中获取所有数量的行?
如果我们不能使用SQL进行我们该如何使用PL/PGSQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态解决方案
SQL需要在呼叫时知道每个结果列的名称和类型(因此是其编号)。您无法使用普通SQL动态地从数据中得出结果列。您可以使用数组或文档类型代替单独的列:
动态解决方案
如果您不够好需要对DB服务器进行两个往返的动态SQL:1。生成SQL。 2。执行SQL。使用
crosstab()
功能从其他模块tablefunc
。 您不熟悉,请首先阅读如果 我们...
的这种形式
2。执行:
...获取:
db<> fiddle 在这里
请参阅:
psql的动态执行,但PSQL
仍然对服务器进行了两个圆形交易,但是只有一个命令。
以下两个解决方案都使用psql meta commands,并且仅在psql中起作用!
使用
\ gexec
使用标准交互式终端,您可以将生成的SQL送回Postgres服务器,直接使用
\ gexec
:相同结果。
使用
\ crosstabview
请参阅(两者相关的答案):
这些解决方案中有很多微妙之处...
除了
假设有一些机制可以禁止重复并直接与关系相矛盾。喜欢:
请参阅:
Static solution
SQL demands to know name and type of each result column (and consequently their number) at call time. You cannot derive result columns from data dynamically with plain SQL. You can use an array or a document type instead of separate columns:
Dynamic solution
If that's not good enough you need dynamic SQL with two round-trips to the DB server: 1. Generate SQL. 2. Execute SQL. Using the
crosstab()
function from the additional moduletablefunc
. If you are unfamiliar, read this first:Returns a query of this form, which we ...
2. Execute:
... to get:
db<>fiddle here
See:
Dynamic execution with psql
Still two round-trips to the server, but only a single command.
Both of the following solutions use psql meta-commands and only work from within psql!
With
\gexec
Using the standard interactive terminal, you can feed the generated SQL back to the Postgres server for execution directly with
\gexec
:Same result.
With
\crosstabview
See (with related answers for both):
There are lots of subtleties in these solutions ...
Aside
Assuming there are some mechanisms in place to disallow duplicates and directly contradicting relationships. Like:
See: