简体   繁体   English

需要MySQL查询协助

[英]MySQL query assistance required

It's either been a long day or maybe it's time to retire :-) 这是漫长的一天,或者也许是该退休的时候了:-)

I have a table of transactions of either purchase( id = 2) or allocation( id= 1) on registration. 我在注册时有一张购买(id = 2)或分配(id = 1)的交易表。 An allocation transactions occurs first before the customers can then transact. 首先进行分配交易,然后客户才能进行交易。

I am looking to count the conversion rate, which is the number of people who registered and went ahead to make a purchase. 我正在计算转换率,即注册并继续购买的人数。

Here is how my table looks like 这是我的桌子的样子

|  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

Here is the query I have so far 这是我到目前为止的查询

 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

ANy help will be appreciated 任何帮助将不胜感激

Since you want users with both trans types, and there possibly can be multiple of each: 由于您希望用户同时具有两种转换类型,并且每种类型可能会有多个:

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

Basically: select out all the transtype=1 records, then self join to get all the customers that also have the transtype=2 records. 基本上:选择所有transtype = 1记录,然后进行自我联接以获取所有也具有transtype = 2记录的客户。 That'll give you all of your "type 1" customers, and however many "type 2" records also exist for them. 这将为您提供所有的“类型1”客户,但是对于他们来说也存在许多“类型2”记录。 From that you can easily calculate total customers and how many of them actually purchased something. 从中您可以轻松计算出总客户以及实际购买了多少客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM