문제

I'm currently working a statistics program for one of my clients, but i can't seem to figure out how i can calculate a conversion rate.

The table is like:

User_id     order_id    plan_id
12          12          16     
12          13          2
12          14          2
14          15          5
14          16          16
15          17          16

Order id 16 is a tryout, the rest is paid. So you can see that user 15, only has a tryout. So he didnt convert free into paid.

User 12 and 14 both have a tryout (plan_id=16) and a paid plan (plan_id<>16) meaning he converted from free to paid.

So i'm looking for a direction pointer for calculating conversion rate. I'm looking for a query (or amounts that output what i need) in the direction of:

Converted   Notconverted
1200        1800

This is for users. So how many users did paid for an account that had a try out.

I had this query; but i can't seem to group it on the users because it return more rows, obviously.

SELECT (SELECT COUNT(*) AS TotalOrders FROM orders where (plan_id<>17) GROUP BY user_id) AS SuccessCount, (SELECT COUNT(*) AS TotalFreeAccounts FROM orders where (plan_id=16) GROUP BY user_id) AS TrialCount


Edit: Almost there (:

So far we came across the following query, which seems to be working for the converted. But not for the non-converted. As a minor adjustment, plan 17 is also a tryout:

SELECT 
    count(Distinct TN2.user_id) as Converted,
    count(Distinct TN.user_id) as NotConverted
FROM orders TN
INNER JOIN orders TN2
 on TN.user_id  = TN2.user_id
 and (TN.plan_id = 16 OR TN.plan_id = 17)
 and (TN2.plan_id <> 16 AND TN2.plan_id<>17)

The outcome is exactly the same, but this isn't possible

it both is set to the same number, that could be possible. I'm pretty sure that is correct for the non-converted.

Thanks for your help in advance.

도움이 되었습니까?

해결책

Assuming I understand the question:

Used self join to find users who had tryouts (Plan 16) and join to the same user who had a plan that wasn't a tryout (not plan 16)

LEFT JOIN used as we're interested in those that started on TryOut and then MAY have became a non tryoutplan. We didn't care about those users who didn't have a tryout.

Distinct was used as we didn't care what non-try-out-plan in our counts, just any plan.

So in your example:

User_id     order_id    plan_id
12          12          16     
12          13          2
12          14          2
14          15          5
14          16          16
15          17          16

RESULTS of the below query should yield (assuming logic and syntax are sound)

CONVERTED  NOTCONVERTED
2          1

user 15 had a tryout plan, but no others, thus isn't converted. user 12 had a tryout plan and multiple non tryouts so only gets counted once. user 14 had a tryout plan and one other plan, thus gets counted once.

SELECT count(Distinct TN2.user_ID) as Converted, 
       count(Distinct TN.User_ID) as NotConverted   
FROM tableName TN
LEFT JOIN TableName TN2
 on TN.User_ID  = TN2.user_ID
 and TN.Plan_ID = 16
 and TN2.Plan_ID <> 16
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top