Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQL解析引用链接_Sql_Database_Amazon Redshift_Utm_Dbvisualizer - Fatal编程技术网

如何使用SQL解析引用链接

如何使用SQL解析引用链接,sql,database,amazon-redshift,utm,dbvisualizer,Sql,Database,Amazon Redshift,Utm,Dbvisualizer,世界你好 我有一个数据源来存储我所有的网站会话数据,我希望分析推荐URL并将它们分为五类:主页(www.mywebsite.com)、子网站(www.mywebsite.com/employees)、外部流量、谷歌、雅虎,Bing、Facebook、Linkedin和Youtube 为了做到这一点,我必须解析引用URL。我已经找到了部分答案,但我当前的查询错误地将来自外部网站的URL分类,这些网站的URL中包含我们的域。当我的查询运行时,它不会将这些url分组为外部流量,而是创建自己的分组作为引

世界你好

我有一个数据源来存储我所有的网站会话数据,我希望分析推荐URL并将它们分为五类:主页(www.mywebsite.com)、子网站(www.mywebsite.com/employees)、外部流量谷歌雅虎BingFacebookLinkedinYoutube

为了做到这一点,我必须解析引用URL。我已经找到了部分答案,但我当前的查询错误地将来自外部网站的URL分类,这些网站的URL中包含我们的域。当我的查询运行时,它不会将这些url分组为外部流量,而是创建自己的分组作为引用url的名称

例如,使用此URL,您可以看到我的网站名称是如何嵌入到他们的URL中的:

https://www.helpthepeople.com/redirect.action?link=https%3A%2F%2F**www.mywebsite.com**%2Femployers%2Fblog%2Fwhat-to-do-when-asking-for-help%2F&encoded=lFAJCUeGqgrDkdlYfDwwbEfCqGlV
我得到以下输出:

www.helpthepeople.com
期望输出:

Outside Traffic
在大多数情况下,我的查询是有效的,但只有当上面的例子出现时,我才有问题。有谁知道写这篇文章的更好方法吗?我的问题如下:

SELECT 
    CASE 
        WHEN referrer_page LIKE '%mywebsite.com%' 
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%mywebsite.com/employees%' 
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'.com/',2),'/',1)
         WHEN referrer_page LIKE '%google%' 
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%yahoo%' 
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%bing%' 
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%facebook%'
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%linkedin%'
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
        WHEN referrer_page LIKE '%youtube%'
            THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)  
        ELSE 'outside_referral_traffic' 
    END AS url_grouping,
    referrer_page,
    session_date,
    channel,
    medium,
    web_source,
    campaign_name,
    id, 
    COUNT (DISTINCT id) AS number_of_sessions
FROM biz_sessions
WHERE session_date >= '2019-07-01' AND session_date <= '2019-07-31'
GROUP BY 
    referrer_page,
    session_date,
    channel,
    medium,
    web_source,
    campaign_name,
    id



选择
案例
当推荐人出现在像“%mywebsite.com%”这样的页面时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人出现在“%mywebsite.com/employees%”这样的页面时
然后分割部分(分割部分(参考页面“.com/”,2),“/”,1)
当推荐人出现在像“%google%”这样的页面上时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人出现在类似“%yahoo%”的页面时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人_页面像“%bing%”时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人出现在像“%facebook%”这样的页面时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人出现在像“%linkedin%”这样的页面时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
当推荐人出现在像“%youtube%”这样的页面时
然后分割部分(分割部分(参考页“//”,2),“/”,1)
其他“外部推荐交通”
以url_分组结束,
推荐人页面,
会议日期:,
频道
中等,
网络资源,
你的名字,
身份证件
将(不同id)计数为会话数
来自商务会议

其中,会话日期>='2019-07-01'和会话日期在
SQL SERVER
中使用
replace
charindex

declare @string varchar(800) = 'https://www.helpthepeople.com/redirect.action?link=https%3A%2F%2Fwww.mywebsite.com%2Femployers%2Fblog%2Fwhat-to-do-when-asking-for-help%2F&encoded=lFAJCUeGqgrDkdlYfDwwbEfCqGlV'

select left(replace(replace(@string,'https://www.helpthepeople.com/redirect.action?link=',''),'https%3A%2F%2F',''),charindex('%2F',replace(replace(@string,'https://www.helpthepeople.com/redirect.action?link=',''),'https%3A%2F%2F',''))-1)

您可以轻松地将其转换为MySQL,您的
CASE
语句在第一个选项中退出,因为它满足了您的网站名称位于整个URL中的类似条件。我会用问号分割推荐URL,以去除所有可能包括您的网站URL和其他内容的参数。查询甚至可能运行得更快

CASE 
    WHEN SPLIT_PART(referrer_page,'?',1) LIKE '%mywebsite.com%' 
    THEN SPLIT_PART(SPLIT_PART(referrer_page,'//',2),'/',1)
    ...
END

更新后,OP与所需的输出不匹配。我是否需要为每个被视为外部流量的URL或字符串声明此选项?我有数百个URL指向该站点的流量。您需要显示完整数据,因为此解决方案适用于较旧版本的OP。