sql - 子查询 GROUP BY 运算符
问题描述
我有一张卡片和帖子表,每个卡片和帖子条目至少有 2 个标签。当前查询使用card_to_tags_link
和post_to_tags_link
表来获取按特定卡片分组的所有帖子。
SELECT
card,
count(post.id) AS count_1,
max(post.date) AS max_1
FROM
card
JOIN card_to_tags_link ON card.id = card_to_tags_link.card_id
JOIN post_to_tags_link ON post_to_tags_link.post_tags @>card_to_tags_link.card_tags
JOIN post ON post_to_tags_link.post_id = post.id
GROUP BY
card.id
现在,因为每个帖子都有一post_owner
列,所以我想author_url
按多个帖子聚合所有帖子并按顺序排列:
SELECT ARRAY(post.author_url
from post
group_by post.author_url
order by count(post.id)
limit by 10)
这是一个单独执行的简单查询,但我想将其作为子查询添加到现有查询中:
SELECT
card,
count(post.id) AS count_1,
max(post.date) AS max_1,
**ADD SUBQUERY HERE**
FROM
card
JOIN card_to_tags_link ON card.id = card_to_tags_link.card_id
JOIN post_to_tags_link ON post_to_tags_link.post_tags @>card_to_tags_link.card_tags
JOIN post ON post_to_tags_link.post_id = post.id
GROUP BY
card.id
是否可以将作者查询作为子查询并将结果作为数组返回?
解决方案
你只是想要ARRAY_AGG()
吗?
SELECT card,
count(post.id) AS count_1,
max(post.date) AS max_1,
ARRAY_AGG(DISTINCT post.author_url)
推荐阅读
- php - 为 woocommerce 结帐条款和条件添加自定义文本而不是页面
- javascript - 通过 Webpack 动态加载/导入拆分的供应商块/捆绑包
- reactjs - 带有反应路由器的奇怪组件生命周期
- sql - 有人可以建议我这个查询有什么问题吗?
- python-3.x - 计算目录中同名的多个文件
- asp.net - 会话超时时创建一个新的 ASP.NET_SessionId
- mysql - 无法在 Docker 上加载 Artifactory
- docker - Docker 灯容器连接到 DB
- apache-spark - 在 Spark 的 saveAsTable 上
- deeplearning4j - DL4J - 使用 ComputationGraph 时,是否可以从中获取类标签?