PostgreSQL中按指定列分组

发布于 2024-12-22 05:54:41 字数 324 浏览 3 评论 0原文

也许,这个问题有点愚蠢,但我很困惑。

如何按指定列对记录进行分组? :)

Item.group(:category_id)

不起作用...

它说:

ActiveRecord::StatementInvalid: PGError: 错误: 列“items.id”必须出现在 GROUP BY 子句中或在聚合函数中使用 第 1 行:按类别_id 从“项目”组中选择“项目”。*

我应该使用哪种聚合函数?

请您提供一个简单的例子。

Maybe, this question is a little stupid, but I'm confused.

How to group records by specified column ? :)

Item.group(:category_id)

does't works...

It says:

ActiveRecord::StatementInvalid: PGError: ERROR: column "items.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "items".* FROM "items" GROUP BY category_id

What kind of aggregate function should i use?

Please, could you provide a simple example.

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

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

发布评论

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

评论(3

输什么也不输骨气 2024-12-29 05:54:41

您必须定义如何对共享相同category_id的值进行分组。将它们连接起来?计算总和?

要创建以逗号分隔的值列表,您的语句可能如下所示:

SELECT category_id
      ,string_agg(col1, ', ') AS col1_list
      ,string_agg(col2, ', ') AS col2_list
FROM   items
GROUP  BY category_id

You need Postgres 9.0 or later for string_agg(col1, ', ')
在旧版本中,您可以用 array_to_string(array_agg(col1), ', ') 替换。更多聚合函数此处

与在客户端中聚合值相比,在 PostgreSQL 中聚合值显然是更好的方法。 Postgres 在这方面非常快并且减少了(网络)流量。

You will have to define, how to group values that share the same category_id. Concatenate them? Calculate a sum?

To create comma-separated lists of values your statement could look like this:

SELECT category_id
      ,string_agg(col1, ', ') AS col1_list
      ,string_agg(col2, ', ') AS col2_list
FROM   items
GROUP  BY category_id

You need Postgres 9.0 or later for string_agg(col1, ', ').
In older versions you can substitute with array_to_string(array_agg(col1), ', '). More aggregate functions here.

To aggregate values in PostgreSQL is the clearly superior approach as opposed to aggregating values in the client. Postgres is very fast at this and it reduces (network) traffic.

素食主义者 2024-12-29 05:54:41

您可以使用 sumavgcount 或任何其他聚合函数。有关此主题的更多信息,您可以在此处找到。

但看来你真的不需要使用SQL分组。

尝试获取所有记录,然后使用 Array#collect 函数按 category_idItem 进行分组

You can use sum, avg, count or any other aggregate function. More on this topic you can find here.

But it seems that you don't really need to use SQL grouping.

Try to fetch all records and then use Array#collect function to group Items by category_id

吖咩 2024-12-29 05:54:41

SQL 中的分组意味着服务器将数据库表中的一个或多个记录分组到一个结果行中。因此,如果您按 category_id 进行分组,则可能有多个与给定类别匹配的记录,因此您不能期望数据库返回表中的所有列(这就是 SELECT * 实际所做的)。

相反,当您使用GROUP BY时,您只能SELECT

  • 分组依据的列和/或
  • 对属于结果组的所有记录执行的聚合函数

根据您的具体需要,相应地修改您的 .select

Grouping in SQL means that the server groups one or more records from the database table into one resulting row. So, if you for example group by category_id, you might have several records matching the given category, so you can't expect the database to return all columns from the table (that's what SELECT * actually does).

Instead, when you use GROUP BY, you can SELECT only:

  • columns you have grouped by, and/or
  • aggregate functions which are performed on all the records belonging to a resulting group

Depending on what you exactly need, modify your .select accordingly.

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