用于获取客户已完成的平均事务数的查询

ttp71kqs  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(221)

我有一个数据库,它有3个表:

Table Name: allowed_users Columns: Id (int) primary, date_joined(datetime)

 Table Name: profile_data Columns: user_id(int) primary, age (int), gender(varchar)

 Table Name: transaction_data Columns: user_id(int) primary, transaction_name(varchar), 
               number_transactions(int),day_of_transaction(date)primary

我想获取一个用户每天所做的事务的平均数量,按他们的性别只允许用户。
我试着这样做:

SELECT
  a.id,
  p.gender,
  AVG(t.number_transactions)
FROM allowed_users AS a
JOIN profile_data AS p
  ON a.id = p.user_id
JOIN transaction_data AS t
  ON t.user_id = p.user_id
GROUP BY gender

我不知道如何每天计算这个。
请告诉我如何按天计算。

kgsdhlau

kgsdhlau1#

试试这个,你会得到用户和性别的平均交易完成。

SELECT allowed_users.id, profile_data.gender, 
(COUNT(transaction_data.id)/COUNT(DISTINCT transaction_data.day_of_transaction)) AS
 averageTransactions 
FROM allowed_users 
INNER JOIN profile_data ON allowed_users.id =profile_data.user_id 
INNER JOIN transaction_data ON allowed_users.id=transaction_data.userId 
GROUP BY allowed_users.id, profile_data.gender

以及用户每天的平均交易量及其性别。

SELECT allowed_users.id, profile_data.gender, 
(COUNT(transaction_data.id)/COUNT(DISTINCT transaction_data.day_of_transaction)) AS
 averageTransactions 
FROM allowed_users 
INNER JOIN profile_data ON allowed_users.id =profile_data.user_id 
INNER JOIN transaction_data ON allowed_users.id=transaction_data.userId 
GROUP BY allowed_users.id, profile_data.gender,transaction_data.day_of_transaction

相关问题