查询以获取客户的平均交易数量

时间:2018-01-11 10:40:38

标签: mysql sql

我有一个有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

我不知道如何每天计算这个。

请建议我如何每天计算。

1 个答案:

答案 0 :(得分:0)

  

尝试此操作,您将获得用户及其性别完成的平均交易。

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