简体   繁体   English

SQL进行渠道分析

[英]SQL for funnel analysis

I have a scenario where people can do action1 and bail out; 我有一个场景,人们可以采取行动1并纾困。 or action1 & action2 & bail out, or they can do action1, action2 & then say "I want to play more". 或action1&action2&bail out,或者他们可以执行action1,action2并说“我想玩更多”。 I want to find number of people at each level: 我想找到每个级别的人数:
- Did action1 -行动1
- Did action1 and action2 -是否执行了action1和action2
- Did action1 and action2 and also said "I want to play more" -做过action1和action2,并说“我想玩更多”

Each time user does an action, we log a sessionId. 每次用户执行操作时,我们都会记录一个sessionId。 So if the use run action 1 and action 2, we will have two rows with same session id, same customer id and but different actions. 因此,如果使用运行操作1和操作2,我们将有两行具有相同的会话ID,相同的客户ID和不同的操作。

╔══════════════════════════════════════╦════════════════╦════════╗
║              SessionId               ║ FirstRunAction ║ UserId ║
╠══════════════════════════════════════╬════════════════╬════════╣
║ 039af321-457e-41a6-b303-41ca935b0877 ║ action_1       ║ eb6    ║
║ 039af321-457e-41a6-b303-41ca935b0877 ║ action_2       ║ eb6    ║
╚══════════════════════════════════════╩════════════════╩════════╝

The above data is in one table. 以上数据在一张表中。 Action1 and action2 are in UserAction table but "I want to play more" is in a separate table called "Play" table. 动作1和动作2在UserAction表中,但“我想播放更多”在另一个表中,称为“播放”表。

I did the following but its not correct. 我做了以下工作,但不正确。

I want to have an if type of logic. 我想要一个if类型的逻辑。 Search for action 2 only when action 1 is there & search for "I want to play more" when that session also has action1 and action2. 仅当动作1存在时才搜索动作2,并且在该会话中同时具有动作1和动作2时搜索“我想玩更多”。 i wrote the one below, but don't know how to store results at any level & if what I am doing is correct. 我在下面写了一个,但是不知道如何在任何级别存储结果以及我在做什么是正确的。 I have 6 million data to start with. 我有600万个数据开始。 Any help? 有什么帮助吗?

Action1Results = SELECT [SessionId]  
                        ,[Action]  
                        ,[UserId]  
                 FROM [Test].[dbo].[UserAction]  
                 WHERE [Action] = 'action_1';  

Action2Results = SELECT [SessionId]  
                        ,[Action]  
                        ,[UserId]  
                 FROM [Test].[dbo].[UserAction]   
                 WHERE [Action] = 'action_2';      

PlayMoreResults = SELECT [SessionId]  
                        ,[Play]  
                        ,[UserId]  
                 FROM [Test].[dbo].[UserPlay]  
                 WHERE [Play] = 'I want to play more';  

FinalResults = SELECT [SessionId]  
                     ,[UserId]  
                 FROM [Test].[dbo].[Action] with (nolock)   
                 INNER JOIN [Test].[dbo].[UserPlay] with (nolock)   
                 ON [Test].[dbo].[UserPlay].SessionId = [Test].[dbo].[Action].SessionId;  

Actually there could be many actions but they will always end with _1 and _2. 实际上可能会有很多动作,但是它们总是以_1和_2结尾。 The list of actions could change over time. 动作列表可能会随着时间而改变。

Eg I can have actions like: 例如,我可以执行以下操作:

'Write_1" , "Write_2", "Birds_1", Birds_2", "Pen_1", "Pen_2". 'Write_1”,“ Write_2”,“ Birds_1”,Birds_2”,“ Pen_1”,“ Pen_2”。

So for these I have to see how many did 所以对于这些我必须看看有多少

'Write_1" -> "Write_2" -> 'I want to play more'', 'Write_1'->“ Write_2”->'我想玩更多”,

then how many did 那有多少

'Birds_1" -> "Birds_2" -> 'I want to play more'', 'Birds_1”->“ Birds_2”->“我想玩更多”,

then how many did 那有多少

'Pen_1" -> "Pen_2" -> 'I want to play more'' 'Pen_1“->” Pen_2“->'我想玩更多”

and so on. 等等。 So funnel for each type. 因此,针对每种类型的渠道。

I assume action can only be ('action_ 1','action_2') other wise you have to compare for those value on the ON-WHERE and the CASE` 我假设动作只能是('action_ 1','action_2') other wise you have to compare for those value on the and the案例中1','action_2') other wise you have to compare for those value on the

SqlFiddle Demo SqlFiddle演示

WITH CTE as (
     SELECT UA1.SessionID, 
            UA1.UserId, 
            UA1.FirstRunAction Action1,
            UA2.FirstRunAction Action2,
            UP.Play Action3
     FROM UserAction UA1
     LEFT JOIN UserAction UA2
            ON UA1.SessionID = UA2.SessionID
           AND UA2.UserId = UA2.UserId
           AND UA1.FirstRunAction <> UA2.FirstRunAction           
     LEFT JOIN UserPlay UP
            ON UA1.SessionID = UP.SessionID
           AND UA2.UserId = UP.UserId
     WHERE UA1.FirstRunAction = 'action_1'
), classify as (
SELECT CASE 
           WHEN Action3 IS NOT NULL AND Action3 = 'I want to play more' THEN 'TYPE 3'
           WHEN Action2 IS NOT NULL THEN 'TYPE 2'
           WHEN Action1 IS NOT NULL THEN 'TYPE 1'
           ELSE 'TYPE 0'
       END as actionType
FROM cte 
)
SELECT actionType, count(*)
FROM classify
GROUP BY actionType

CTE portion OUTPUT CTE部分输出

| SessionID | UserId |  Action1 |  Action2 |             Action3 |
|-----------|--------|----------|----------|---------------------|
|         1 |      1 | action_1 | action_2 | I want to play more |
|         2 |      2 | action_1 |   (null) |              (null) |
|         3 |      3 | action_1 | action_2 |          Don’t Know |
|         4 |      4 | action_1 |   (null) |              (null) |
|         5 |      5 | action_1 | action_2 |              (null) |
  • So UserID = 1 has all three action will be type3 因此, UserID = 1所有三个操作均为type3
  • UserID = (2, 4) only have one action will be type1 UserID = (2, 4)只有一个动作将是type1
  • UserID = 3 has 3 action but last one isnt I want to play more so is type 2 as UserID = 5 UserID = 3有3个动作,但是最后一个不是I want to play more所以输入2作为UserID = 5

Final Output 最终输出

| actionType |   |
|------------|---|
|     TYPE 1 | 2 |
|     TYPE 2 | 2 |
|     TYPE 3 | 1 |

If You have a table USERS you could start with that so also get count of how many user has TYPE 0 如果您有一个表USERS ,则可以从中开始,这样也可以计算出有多少用户拥有TYPE 0

This should give you a count of distinct users by each session_category - 这应该为您提供每个session_category不同用户的数量-

  3 = I want to play more
  2 = action_1, and action_2
  1 = action_1

Note this will count a user multiple times across session_categories if the user in fact does only action_1 on one occasion, and action_1+action_2 on another. 请注意,如果用户实际上一次仅执行action_1,而一次仅执行action_1 + action_2,则这将在session_categories中对用户进行多次计数。 You don't explain how you plan to deal with this situation (unless I missed something). 您无需解释如何计划应对这种情况(除非我错过了什么)。

https://gist.github.com/leonpanokarren/56f313130118dad47113 https://gist.github.com/leonpanokarren/56f313130118dad47113

I tried pasting this query inline here a gazillion times in vain. 我尝试在此内联粘贴此查询,但徒劳无数。

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

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