简体   繁体   English

Postgresql get 列出所有有 3 个或更多记录交易的客户

[英]Postgresql get List all customer who has 3 or more record transaction

Iam Using postgreSQL.我正在使用 postgreSQL。 I have 2 table with relations我有 2 张关系表

table customer:表客户:

-------------------------------------------
| customer_id (PK) | first_name | last_name  |
-------------------------------------------
| 1                | ogi        |    tampoo  |
| 2                | cimol      |    comet   |
| 3                | cocang     |    tampoo  |
| 4                | bedu       |    comet   |
| 5                | bonjof     |    tampoo  |

table transaction:表事务:

---------------------------------------------------------
| transaction_id (PK) | customer_id (FK) | total_value  |
---------------------------------------------------------
| 2                   |      1           |    250500    |
| 5                   |      2           |    340600    |
| 4                   |      3           |    150800    |
| 6                   |      4           |     90900    |
| 3                   |      4           |    1009000   |
| 1                   |      5           |    540700    |
| 7                   |      4           |    340000    |

question:问题:

Return all customer who has >1 record transaction.
Obviously from transaction table, customer_id = 4 have 3 records
how to  query this?

Desired result期望的结果

--------------------------------------------------------------------------------------
| customer_id | first_name | last_name  | transaction_id | customer_id  | total_value |
--------------------------------------------------------------------------------------
| 4           | bedu       |    comet   |       5        |      4       |      90900  |
| 4           | bedu       |    comet   |       3        |      4       |     1009000 |
| 4           | bedu       |    comet   |       7        |      4       |     340000  |

what I've try:我尝试过的:

  1. (return nothing) (不返回任何内容)
select cs.* ,tr.*
from 
    customer cs
left join
    transaction tr
    on tr.customer_id = cs.customer_id
group by cs.customer_id , tr.transaction_id
having count(cs.customer_id)>1

  1. ERROR: column "cs.customer_id" must appear in the GROUP BY clause or be used in an aggregate function错误:列“cs.customer_id”必须出现在 GROUP BY 子句中或用于聚合 function
    ERROR: column "tr.transaction_id" must appear in the GROUP BY clause or be used in an aggregate function错误:列“tr.transaction_id”必须出现在 GROUP BY 子句中或用于聚合 function
select *
from 
     customer  cs
left join 
     transaction tr
     on tr.customer_id = cs.customer_id
group by tr.customer_id
having count(tr.customer_id)>1

It appear that (i dont know if it only postgres), It forced to group by each PRIMARY KEY .看来(我不知道它是否只有 postgres),它强制按每个 PRIMARY KEY 分组
When I try to group by any other column which is not PK, it return error and demand the PK for group by .当我尝试按任何其他不是 PK 的列进行分组时,它会返回错误并要求 PK 为group by

Thank you very much.非常感谢。

Your last query almost works.您的最后一个查询几乎可以工作。 Just select the columns from the customer table -- and join by the primary key:只需 select 客户表中的列 - 并通过键连接:

select c.*
from customer c join 
     transaction t
     on t.customer_id = c.customer_id
group by c.customer_id
having count(*) > 1

If you want any columns from t , then you need to use an aggregation function.如果您想要t中的任何列,则需要使用聚合 function。 For instance COUNT(*) to count the number of transactions.例如COUNT(*)来计算事务数。

Also note that I changed the LEFT JOIN to an INNER JOIN .另请注意,我将LEFT JOIN更改为INNER JOIN You require a match in order to meet the HAVING condition, so the outer join is not needed.您需要匹配才能满足HAVING条件,因此不需要外连接。

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

相关问题 如何在SQL服务器中找到只完成一笔交易的客户ID - How to find customer id who has done only one transaction in SQL Server 获得所有分数高于该 class 平均分的学生 - get all students who has got more marks than the average marks of that class 获取最近7天有交易的用户,30天内只有一笔交易 - Get user who has transaction in last 7 days, and only one transaction in 30 days 子查询“查找购买量最大的客户” - Subquery “Finding customer who has the most purchases” 获取给出评论的客户名称 - fetch customer name who has given the review 在同一分支机构中既有帐户又有贷款的客户是谁? - Who is the customer who has both an account and a loan in the same branch? 表X中购买了表Y中所有产品的那些客户ID的列表? - list of those Customer IDs from Table X who have bought all of the products from Table Y? 从具有最低价格的表中获取所有数据 - Get all data from table who has minimum price SQL查找总交易金额最大的Top Customer,列出交易,并在底部的行中合计所有交易 - SQL Find Top Customer with greatest total transaction amount, list transactions, and total all transactions in row at bottom MySql查询:获取在Customer和Customer_x_Billing表中均具有其ID的所有客户 - MySql query: get all customers that has their ID in both Customer and Customer_x_Billing table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM