简体   繁体   English

在 Postgresql 中选择带计数的数据

[英]Select Data with Count in Postgresql

I want to ask how do I select data together with count data.我想问一下我如何选择数据和计数数据。

In this case, I want the user to appear and the number of transactions that he has.在这种情况下,我希望用户出现以及他拥有的交易数量。

Like this code that I made.就像我制作的这段代码。

SELECT "transaction"."user_id",
COUNT(transaction.id) trans_count
FROM transaction 
inner join "users" on "users"."id" = "transaction"."user_id"
GROUP BY user_id

The code above successfully selects user_id and trans_count , but when I am trying to show users.name上面的代码成功选择了user_idtrans_count ,但是当我尝试显示users.name

this error message appears.出现此错误消息。

Error in query: ERROR: column "users.name" must appear in the GROUP BY clause or be used in an aggregate function LINE 3: "users"."name"查询错误:错误:列“users.name”必须出现在 GROUP BY 子句中或用于聚合函数第 3 行:“users”.“name”

Is it true that I am cannot select other data when I count data or is there a better way ?.我在统计数据时无法选择其他数据是真的还是有更好的方法?。

Thank You.谢谢你。

You can include user.name in the group by :您可以group by以下方式在group by包含user.name

SELECT "transaction"."user_id",
"user"."name",
COUNT(transaction.id) trans_count
FROM transaction 
inner join "users" on "users"."id" = "transaction"."user_id"
GROUP BY "transaction"."user_id", "user"."name"

Otherwise, when the DBMS tries to combine ( group ) multiple rows into a single row, it doesn't know which name value should it pick, which is why it throws the error.否则,当 DBMS 尝试将多行组合( group )为一行时,它不知道应该选择哪个name值,这就是它抛出错误的原因。

In this case, user_id and user.name have a one-to-one mapping, so you can simply include name in the group by clause.在这种情况下, user_iduser.name具有一对一的映射关系,因此您可以简单地在group by子句中包含name

Otherwise you'd have to tell the DBMS how to select one value from the multiple records that are in each group, eg:否则,您必须告诉 DBMS 如何从每组中的多个记录中选择一个值,例如:

min(user.name) or max(user.name) min(user.name)max(user.name)

SELECT "transaction"."user_id",
min("user"."name") user_name,
COUNT(transaction.id) trans_count
FROM transaction 
inner join "users" on "users"."id" = "transaction"."user_id"
GROUP BY "transaction"."user_id"

When every your using GROUP BY You must be group by all column which is fetching or use Aggregate Functions当您使用GROUP BY您必须按所有正在获取或使用聚合函数的列进行分组

Group by (same as @rohitvats)分组依据(与@rohitvats 相同)

GROUP BY "transaction"."user_id", "user"."name"

---- OR ---- - - 或者 - -

Aggregate Functions MAX() , MIN()聚合函数MAX() , MIN()

SELECT "transaction"."user_id",
MAX("user"."name") as name,
COUNT(transaction.id) trans_count
FROM transaction 
inner join "users" on "users"."id" = "transaction"."user_id"
GROUP BY "transaction"."user_id"

Your code would work if you aggregated by the users.id :如果您按users.id聚合,您的代码将起作用:

SELECT u.id, u.user_name, COUNT(*) as trans_count
FROM users u JOIN
     transaction t
     ON t.id = u.user_id
GROUP BY u.id;

(I removed the double quotes because they clutter the logic and are not necessary to explain what is going on.) (我删除了双引号,因为它们使逻辑混乱,不需要解释发生了什么。)

Why?为什么? Presumably, users.id is unique (or equivalently the primary key).据推测, users.id是唯一的(或等效的主键)。 Postgres supports aggregating by a unique key in a table and also including unaggregated columns in the SELECT . Postgres 支持按表中的唯一键进行聚合,还支持在SELECT包含未聚合的列。 This is an implementation of "functional dependent" aggregations from the SQL standard.这是 SQL 标准中“功能相关”聚合的实现。

When you use transactions.user_id , Postgres does not recognize the functional dependence (even though you might think that the ON clause would imply it).当您使用transactions.user_id ,Postgres 无法识别功能依赖(即使您可能认为ON子句会暗示它)。 So, your code doesn't work.所以,你的代码不起作用。

The alternative is to add user_name to the GROUP BY as well.另一种方法是将user_name添加到GROUP BY中。 However, your version almost works if you use the column from the correct table.但是,如果您使用正确表中的列,您的版本几乎可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM