knex骨料排在独特的或组中

发布于 2025-02-01 19:30:35 字数 689 浏览 4 评论 0 原文

我有一个comment_retport的数据集,在注释_REPORT上,我可以有多个用户报告相同的评论ID。不喜欢从数据库中选择所有报告的评论,分组评论ID并将记者名称组合成

//comments_reported table

COMMENT_ID   REPORTER     REPORT_REASON
149          Eddy         spam
150          John         inappropriate
150          Tim          spam
151          Don          rude
152          Dave         explicit
//Output ill like to select from the table distinctOn("COMMENT_ID") or groupby("COMMENT_ID")
COMMENT_ID   REPORTER     REPORT_REASON
149          Eddy         spam
150          John, Tim    inappropriate
151          Don          rude
152          Dave         explicit

不喜欢从comment_repted中选择全部,但汇总了名称字段

I have a dataset of comments_reported, on the comments_reported, I can have multiple users who have reported the same comment id. Ill like to select all reported comments from the database, groupedby comment id and combine the reporters names

//comments_reported table

COMMENT_ID   REPORTER     REPORT_REASON
149          Eddy         spam
150          John         inappropriate
150          Tim          spam
151          Don          rude
152          Dave         explicit
//Output ill like to select from the table distinctOn("COMMENT_ID") or groupby("COMMENT_ID")
COMMENT_ID   REPORTER     REPORT_REASON
149          Eddy         spam
150          John, Tim    inappropriate
151          Don          rude
152          Dave         explicit

Ill like to select all from comments_reported but aggregate the name field

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

季末如歌 2025-02-08 19:30:35

您的数据我更改您的数据以获取所需的结果

CREATE TABLE comments_reported (
   COMMENT_ID    INTEGER  NOT NULL 
  ,REPORTER      VARCHAR(100) NOT NULL
  ,REPORT_REASON VARCHAR(100) NOT NULL
);
INSERT INTO comments_reported 
(COMMENT_ID,REPORTER,REPORT_REASON) VALUES 
(149,'Eddy','spam'),
(150,'John','inappropriate'),
(150,'Tim','inappropriate'),--spam =>inappropriate
(151,'Don','rude'),
(152,'Dave','explicit');

使用 String_agg

SELECT comment_id,
       String_agg(reporter, ',') reporter,
       report_reason
FROM   comments_reported
GROUP  BY comment_id ,report_reason 

your data I change your data to get your desired result

CREATE TABLE comments_reported (
   COMMENT_ID    INTEGER  NOT NULL 
  ,REPORTER      VARCHAR(100) NOT NULL
  ,REPORT_REASON VARCHAR(100) NOT NULL
);
INSERT INTO comments_reported 
(COMMENT_ID,REPORTER,REPORT_REASON) VALUES 
(149,'Eddy','spam'),
(150,'John','inappropriate'),
(150,'Tim','inappropriate'),--spam =>inappropriate
(151,'Don','rude'),
(152,'Dave','explicit');

use string_agg

SELECT comment_id,
       String_agg(reporter, ',') reporter,
       report_reason
FROM   comments_reported
GROUP  BY comment_id ,report_reason 

dbfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文