Postgres 按查询分组

2024-05-23

我正在尝试在 postgres 的查询中使用 group by 。我无法让它按照我想要的方式工作,以便根据需要对结果进行分组。

这是另一个堆栈问题的扩展我刚刚回答过的递归查询 https://stackoverflow.com/questions/13299091/postgresql-ordering-records-in-a-hierarchal-query-result。但现在我需要能够对最终查询的 root_id 列上的结果进行分组。这是之前的查询:

select cl.parent_comment_id, 
     cl.article_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
order by cl.root_id, cl.article_comment_id, cl.comment_depth;

这是我想要做的,以便将具有相同parent_comment_id 的任何记录保存在一起。

select cl.parent_comment_id, 
     cl.article_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
group by cl.parent_comment_id
order by cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;

可以有许多记录返回相同的parent_comment_id,但对于任何给定的article_comment_id 记录不同。即每个评论都是唯一的(id、评论、标题等),但每个父评论可以有许多子评论。这已经通过递归查询检索到,现在我只是尝试将它们正确分组。

Edit:


看一眼http://www.sqlfiddle.com/#!12/77771/2 http://www.sqlfiddle.com/#!12/77771/2.我希望article_comment_id=6紧跟在article_comment_id=3下面,因为id=3是父级。然后article_comment_id=4。

但我认为这需要按程序完成。

所以我认为这是一个“没关系”类型的问题,除非有人知道如何做(这就是我将其保留的原因)。但我想我会尝试从程序上解决这部分问题。


对于递归查询,您可以使用此技巧和 0 填充的字符串创建分层路径:SQL小提琴 http://www.sqlfiddle.com/#!12/77771/12/0

with recursive comment_list(article_comment_id, parent_comment_id, comment, article_id, comment_depth, comment_path) AS (
    select c.article_comment_id, 
           c.parent_comment_id, 
           c.comment, 
           c.article_id, 
           c.comment_depth,
           substr(CAST(1000000000+c.article_comment_id as varchar(1000)),2)
    from test_comment c
    where article_id = 100
      and parent_comment_id = 0

  union all

    select c.article_comment_id, 
           c.parent_comment_id, 
           c.comment, 
           c.article_id, 
           c.comment_depth,
           cl.comment_path || substr(CAST(1000000000+c.article_comment_id as varchar(1000)),2)
    from test_comment c
       join comment_list cl on c.parent_comment_id = cl.article_comment_id
)
select cl.article_comment_id,
     cl.comment_path, 
     cl.parent_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
order by cl.comment_path, cl.article_comment_id, cl.comment_depth;

删除 GROUP BY。您想要将它们“分组”以进行显示,这实际上是“ORDER BY”

select cl.parent_comment_id, 
     cl.article_comment_id,
     cl.comment, 
     cl.article_id,
     cl.comment_depth
from comment_list cl
order by cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;

您可能仍然需要也可能不需要cl.root_id按顺序排列,所以可以是

order by cl.root_id, cl.parent_comment_id, cl.article_comment_id, cl.comment_depth;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Postgres 按查询分组 的相关文章

随机推荐

  • 可以使用哪些技术来衡量 pandas/numpy 解决方案的性能

    Question 如何简洁全面地衡量下面各个功能的性能 Example 考虑数据框df df pd DataFrame Group list QLCKPXNLNTIXAWYMWACA Value 29 52 71 51 45 76 68 6
  • 创建单个随机 Magento 优惠券

    我遇到了一些麻烦 我想要做的是每次有人订阅我们的时事通讯时 在 Magento 中自动生成一个随机优惠券代码 这张优惠券可减 10 美元 并且有exp 订阅后两周的日期 因此 我正在尝试编写一个简单的脚本 当提交 订阅我们的时事通讯 表单时
  • Javascript等待/异步执行顺序

    所以我试图把我的头脑集中在 Promise await async 上 我不明白为什么当 go 执行时 带有 finished 的警报会紧随 console log coffee 之后 当所有函数都使用等待 承诺时 为什么它只等待 getC
  • 如何在多个视图中显示相同的导航栏?

    我可以为一个视图重现以下导航栏 但是 一旦我单击按钮后移至下一个视图 我就会丢失最右边的两个图标 搜索 个人资料 据我所知 从故事板设置导航项目通常是按视图进行的 我可以为每个视图复制这些项目 但我想知道是否有更好的方法来完成一次 是否有教
  • 如何在多线程应用程序中安全地填充数据并 Refresh() DataGridView?

    我的应用程序有一个 DataGridView 对象和一个 MousePos 类型的列表 MousePos 是一个自定义类 它保存鼠标 X Y 坐标 类型为 Point 和该位置的运行计数 我有一个线程 System Timers Timer
  • Xpages 让 Select 2 与当前扩展库一起使用

    我已经安装了最新的扩展库 并正在使用 Bootstrap 构建 Xpages 应用程序 Select 2 不包含在当前的 Ext Lib 中 它包含在 Xpages4Bootstrap 中 虽然这是一个很好的扩展 但我不确定我是否真的应该包
  • Google API 和 OAuth 2.0 的正确重定向 URI

    我正在使用 Google Maps API 制作一个应用程序 我希望能够让一个人在一台计算机上观看另一个人在地图上编辑的内容 我正在考虑将地图信息传递到 Google Fusion Table 另一个人将能够将所有内容视为一层 我希望该程序
  • 为什么 __dict__ 和 __weakref__ 类从未在 Python 中重新定义?

    类创建似乎从来没有re 定义 dict and weakref class属性 即 如果它们已经存在于超类的字典中 则它们不会添加到其子类的字典中 但始终re 定义 doc and module class属性 为什么 gt gt gt c
  • jetpack compose 是否使用drawable-night 文件夹?

    我们有一个基于视图的 Android 应用程序 其中有一些可绘制对象res drawable文件夹 以及夜间模式的对应文件夹res drawable night folder 使用旧视图时 引用可绘制对象R drawable foo从 XM
  • 如何从 Boost.PropertyTree 复制子树

    我有一些boost property tree ptree 我需要树来删除一些具有特定标签名称的元素 例如 xml 表示源ptree如下
  • 从请求url获取hash参数

    我有这样的网址 http www coolsite com daily plan id 1 http www coolsite com daily plan id 1解析该字符串并读取哈希值 id 之后的值 的最简单方法是什么 谢谢 在客户
  • 如何使用foldr为列表创建显示实例?

    我想为我的数据类型 我的列表 编写自己的显示实例 到目前为止 我的方法是有效的 但我总是在末尾有一个逗号 我已经尝试用最后一个元素启动折叠并将其从列表中删除 但它很麻烦而且不起作用 有没有更简单的方法来获得正确的解决方案 实际 1 2 3
  • F# 和 MEF:导出函数

    因此 我试图在 F 控制台应用程序中运行这个简单的测试 open System Reflection open System ComponentModel Composition open System ComponentModel Com
  • 未捕获的类型错误:对象 # 在 Chrome 中没有“查找”方法

    可能与 未捕获的类型错误 对象 没有方法 查找 https stackoverflow com q 11134646 561731 这是我的问题的讨论的聊天记录 https chat stackoverflow com rooms 17 c
  • Safari 扩展应用程序未显示在 Safari 首选项“扩展”选项卡中

    我已遵循以下提到的所有说明创建您的第一个 Safari 扩展应用程序 https developer apple com library content documentation NetworkingInternetWeb Concept
  • MySQL 连接器 C++ 64 位在 Visual Studio 2012 中从源代码构建

    我正在尝试建立mySQL 连接器 C 从源头在视觉工作室2012为了64 bit建筑学 我知道这取决于一些boost头文件和C 连接器 跑步CMake生成一个项目文件 但该项目文件无法编译 因为有一大堆非常令人困惑的错误 这些错误可能与包含
  • 如何将项目插入到特定索引处的空数组中?

    我想将一个项目插入到空数组的指定索引中 我看到有 Array prototype splice 方法 但是 如果我在空数组上使用 splice 它只会添加项目来结束数组 如下所示 var a a splice 3 0 item 3 cons
  • 从使用heroku发送的邮件中删除“via sendgrid.me”

    我正在使用免费的 sendgrid 计划从 Heroku 上托管的 Rails 应用程序发送电子邮件 我使用以下组合进行设置这些说明 http devcenter heroku com articles sendgrid and 本教程 h
  • 如何使用 Javascript OAuth 库不暴露您的密钥?

    看着Twitter OAuth 库 https dev twitter com docs twitter libraries 我看到了这个注释 将 JavaScript 与 OAuth 结合使用时要小心 不要暴露你的钥匙 然后 看着jsOA
  • Postgres 按查询分组

    我正在尝试在 postgres 的查询中使用 group by 我无法让它按照我想要的方式工作 以便根据需要对结果进行分组 这是另一个堆栈问题的扩展我刚刚回答过的递归查询 https stackoverflow com questions