编写查询以按顾客购买的产品数量显示顾客的分布

时间:2019-08-07 19:22:44

标签: sql

写一个查询以按顾客购买的产品数量显示顾客的分布。

客户交易表– CUSTOMER_TXNS

Column Name Description                              Type

Account_id  Account identifier                      Integer
Txn_timestamp   Time of transaction (UTC)               Timestamp
Product_id  The id of the product purchased             Integer
Txn_Amt         The revenue amount of the transaction       Float
Txn_Qty The number of items purchased                       Integer

注意:客户交易记录表的每个account_id都有多个记录。

Customer Master Table – CUSTOMER_MSTR

Column Name         Description                        Type
Account_id          Account identifier                 Integer
Country Country         Code                                   Character(3)
Address                 Address of the customer                Character(64)
Registerd_Dt            Date the account id was first used     Date
Tier                    Account Tier                           Integer

注意:“客户主”表的每个account_id都有一条记录。

1 个答案:

答案 0 :(得分:0)

您可以使用两种聚合级别:

select cnt, count(*), min(account_id), max(account_id)
from (select account_id, count(*) as cnt
      from customer_txns
      group by account_id
     ) a
group by cnt
order by cnt;

当我运行这种类型的查询时,我希望为每个计数提供示例帐户。

相关问题