SQL跟踪客户的旅程

时间:2018-08-25 15:49:09

标签: sql analytics

我有访问网站的用户数据,然后他们注册,登录并继续。因此,当它们降落在页面上时,user_id将为空。用户注册后,将从跟踪器生成一个user_id。例如。

user_id   IP-address      page_url         domain_id   location
null      192.45.34.23    Landing page     A           ABC
null      192.45.34.23    Welcome page     A           ABC
null      192.45.34.23    FAQ              A           ABC
null      192.45.34.23    Help             A           ABC
null      178.34.12.54    Landing page     A           ABC
12345     192.45.34.23    Sign up          A           ABC
12345     192.45.34.23    Sign in-page1    A           ABC
12345     178.34.12.54    Sign in-page 2   A           ABC   

在跟踪客户的旅程时,我需要将分配有空值的user_id映射为一个。 因此,在表中,如果IP地址和12345相同,则top5用户ID必须为domain_id

用户12345具有2个IP地址,因此当IP地址或domain_id匹配时,必须映射用户ID。

输出应如下所示

user_id   IP-address      page_url         domain_id     location
12345     192.45.34.23    Landing page     A             ABC
12345     192.45.34.23    Welcome page     A             ABC
12345     192.45.34.23    FAQ              A             ABC
12345     192.45.34.23    Help             A             ABC
12345     178.34.12.54    Landing page     A             ABC
12345     192.45.34.23    Sign up          A             ABC
12345     192.45.34.23    Sing in-page 1   A             ABC
12345     178.34.12.54    Sign in-page 2   A             ABC 

1 个答案:

答案 0 :(得分:0)

这是您的追求吗?这对SQL Server使用T-SQL语法。但是应该与大多数其他数据库引擎所需的资源相似。

declare @example table
(
  Id bigint not null identity(1,1)
  , [user_id] bigint 
  , [IP-address] nvarchar(15) not null
  , page_url nvarchar(1024) not null
  , domain_id  nvarchar(32) not null
  , location nvarchar(32) not null
)

insert @example([user_id],   [IP-address], page_url, domain_id, location)
values (null   , '192.45.34.23', 'Landing page'  , 'A', 'ABC')
     , (null   , '192.45.34.23', 'Welcome page'  , 'A', 'ABC')
     , (null   , '192.45.34.23', 'FAQ'           , 'A', 'ABC')
     , (null   , '192.45.34.23', 'Help'          , 'A', 'ABC')
     , (null   , '178.34.12.54', 'Landing page'  , 'A', 'ABC')
     , (12345  , '192.45.34.23', 'Sign up'       , 'A', 'ABC')
     , (null   , '123.1.2.3'   , 'Help'          , 'B', 'ABC')
     , (12345  , '192.45.34.23', 'Sign in-page1' , 'A', 'ABC')
     , (12345  , '178.34.12.54', 'Sign in-page 2', 'A', 'ABC')
     , (98765  , '123.1.2.3'   , 'Help'          , 'B', 'ABC')

select Id
, coalesce
(
    [user_id]
    ,
    (
        select top 1 [user_id] 
        from @example b
        where [user_id] is not null
        and b.Id > a.Id 
        and
        (
            b.[IP-address] = a.[IP-address]
            or b.domain_id = a.domain_id
        )
        order by Id
    )
)      [user_id]
,      [IP-address]
,      page_url
,      domain_id
,      location
from @example a

即我们从表格中选择所有行,但是在未填充用户ID的情况下,我们使用您指定的条件(即在域或IP上匹配)从后面的条目中派生用户ID。

您没有在您的条件中指定它,但是我自由地添加了一个ID字段,并使用它来确保我们提取的条目是以后的条目,并且是第一个匹配的条目。假设空值只会在用户登录之前发生(即用户不会注销并继续访问该站点)。包括此内容而不进行任何匹配的原因是,我认为随着时间的流逝,可能会将同一IP重新分配给其他用户。因此,您可以最好地猜测出多个匹配项中的哪个最有可能是您所追求的用户。

相关问题