如何像 SQL Server 一样在 select 子句中使用 join in from 子句执行 Postgresql 子查询?
我正在尝试在 postgresql 上编写以下查询:
select name, author_id, count(1),
(select count(1)
from names as n2
where n2.id = n1.id
and t2.author_id = t1.author_id
)
from names as n1
group by name, author_id
这当然可以在 Microsoft SQL Server 上运行,但在 postegresql 上根本不起作用。我读了一下它的文档,似乎我可以将其重写为:
select name, author_id, count(1), total
from names as n1, (select count(1) as total
from names as n2
where n2.id = n1.id
and n2.author_id = t1.author_id
) as total
group by name, author_id
但这会在 postegresql 上返回以下错误:“FROM 中的子查询无法引用相同查询级别的其他关系”。所以我被困住了。有谁知道我怎样才能实现这一目标?
谢谢
I am trying to write the following query on postgresql:
select name, author_id, count(1),
(select count(1)
from names as n2
where n2.id = n1.id
and t2.author_id = t1.author_id
)
from names as n1
group by name, author_id
This would certainly work on Microsoft SQL Server but it does not at all on postegresql. I read its documentation a bit and it seems I could rewrite it as:
select name, author_id, count(1), total
from names as n1, (select count(1) as total
from names as n2
where n2.id = n1.id
and n2.author_id = t1.author_id
) as total
group by name, author_id
But that returns the following error on postegresql: "subquery in FROM cannot refer to other relations of same query level". So I'm stuck. Does anyone know how I can achieve that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定我是否完全理解您的意图,但也许以下内容会接近您想要的:
不幸的是,这增加了按 id 以及 name 和author_id 对第一个子查询进行分组的要求,我认为这是不需要的。不过,我不确定如何解决这个问题,因为您需要有 id 才能加入第二个子查询。也许其他人会想出更好的解决方案。
I'm not sure I understand your intent perfectly, but perhaps the following would be close to what you want:
Unfortunately this adds the requirement of grouping the first subquery by id as well as name and author_id, which I don't think was wanted. I'm not sure how to work around that, though, as you need to have id available to join in the second subquery. Perhaps someone else will come up with a better solution.
补充 @Bob Jarvis 和 @dmikam 答案,当您不使用 LATERAL 时,Postgres 不会执行良好的计划,在模拟下面,在这两种情况下查询数据结果相同,但成本却截然不同
表结构
在不使用
LATERAL
的子查询中使用GROUP BY
执行JOIN
结果
使用
LATERAL< 的 /code>
结果
我的 Postgres 版本是
PostgreSQL 10.3 (Debian 10.3-1.pgdg90+1)
Complementing @Bob Jarvis and @dmikam answer, Postgres don't perform a good plan when you don't use LATERAL, below a simulation, in both cases the query data results are the same, but the cost are very different
Table structure
Performing
JOIN
withGROUP BY
in subquery withoutLATERAL
The results
Using
LATERAL
Results
My Postgres version is
PostgreSQL 10.3 (Debian 10.3-1.pgdg90+1)
我知道这已经很旧了,但是自从 Postgresql 9.3< /a> 有一个选项可以使用关键字“LATERAL”在 JOINS 内部使用 RELATED 子查询,因此问题中的查询如下所示:
I know this is old, but since Postgresql 9.3 there is an option to use a keyword "LATERAL" to use RELATED subqueries inside of JOINS, so the query from the question would look like:
我只是根据上面评论中发布的 Bob Jarvis 答案,用我需要的最终 sql 的格式化版本来回答:
I am just answering here with the formatted version of the final sql I needed based on Bob Jarvis answer as posted in my comment above:
如果更多的内连接使用
distinct
,因为更多的连接组性能很慢used
distinct
if more inner join, because more join group performance is slow