[英]MySQL query assistance required
这是漫长的一天,或者也许是该退休的时候了:-)
我在注册时有一张购买(id = 2)或分配(id = 1)的交易表。 首先进行分配交易,然后客户才能进行交易。
我正在计算转换率,即注册并继续购买的人数。
这是我的桌子的样子
| id | transaction_type_id | customer_id | created_at |
| 234 | 1 | 22 | 2015-11-26
| 235 | 2 | 22 | 2015-11-26
| 236 | 1 | 23 | 2015-11-27
| 237 | 1 | 24 | 2015-11-27
| 238 | 1 | 25 | 2015-11-27
| 239 | 1 | 26 | 2015-11-28
| 240 | 2 | 26 | 2015-11-28
| 241 | 1 | 27 | 2015-11-28
| 242 | 1 | 28 | 2015-11-28
这是我到目前为止的查询
SELECT COUNT(t.id) AS total, DATE(t.transaction_date) as trans_date, (SELECT COUNT(t1.id) FROM transactions t1 WHERE t1.transaction_type_id = 1 AND t1.member_id = t.member_id) AS converted FROM transactions t WHERE t.transaction_type_id = 21 AND DATE(t.transaction_date) >= DATE(CURRENT_TIMESTAMP())-7 GROUP BY trans_date ORDER BY trans_date ASC
由于您希望用户同时具有两种转换类型,并且每种类型可能会有多个:
SELECT t1.customer_id, count(t2.transaction_type_id)
FROM yourtable t1
LEFT JOIN yourtable t2 ON
(t1.customer_id = t2.customer_id AND t2.transaction_type_id = 2)
WHERE t1.transaction_type_id = 1
基本上:选择所有transtype = 1记录,然后进行自我联接以获取所有也具有transtype = 2记录的客户。 这将为您提供所有的“类型1”客户,但是对于他们来说也存在许多“类型2”记录。 从中您可以轻松计算出总客户以及实际购买了多少客户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.