[英]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.