【问题标题】:Postgresql get List all customer who has 3 or more record transactionPostgresql 获取列出所有拥有 3 条或更多记录交易的客户
【发布时间】:2021-03-13 09:05:45
【问题描述】:

我正在使用 postgreSQL。 我有 2 张关系表

表客户:

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

表交易:

---------------------------------------------------------
| 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    |

问题:

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

想要的结果

--------------------------------------------------------------------------------------
| 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  |

我的尝试:

  1. (不返回任何内容)
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. 错误:列“cs.customer_id”必须出现在 GROUP BY 子句中或用于聚合函数
    错误:列“tr.transaction_id”必须出现在 GROUP BY 子句中或用于聚合函数中
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

看来(我不知道它是否只有 postgres),它强制按每个 PRIMARY KEY 分组。
当我尝试按任何其他不是 PK 的列进行分组时,它会返回错误并要求 group by 的 PK。

非常感谢。

【问题讨论】:

    标签: sql postgresql group-by left-join having-clause


    【解决方案1】:

    您的最后一个查询几乎有效。只需从客户表中选择列 - 并通过 primary 键加入:

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

    如果您想要来自t 的任何列,那么您需要使用聚合函数。比如COUNT(*)来统计交易数量。

    另请注意,我将LEFT JOIN 更改为INNER JOIN。您需要匹配才能满足HAVING 条件,因此不需要外连接。

    【讨论】:

    • 哇这么快:D。是的,根据我的需要,我也想知道如何填充事务表的列,因为我知道我还不了解聚合函数。
    猜你喜欢
    • 2019-08-14
    • 2017-09-07
    • 1970-01-01
    • 2023-01-05
    • 2022-09-23
    • 2017-04-08
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    相关资源
    最近更新 更多