我正在尝试创建一个查询,以通过连接3个表来显示前24个浏览量最高的页面。
但是,我无法使其正常工作。通常,使用UNION
,JOIN
或编写的函数/脚本的一部分都有问题。
这些表是:
+---------------+
| dbo_Good_URLs |
+---------------+
| Url |
| HTTPAlias |
| PortalID |
| page_title |
+---------------+
+-----------------+
| dbo_vw_GoodURLs |
+-----------------+
| URL |
| PortalID |
| HTTPAlias |
| Title |
+-----------------+
+-----------------------+
| dbo_analytics_history |
+-----------------------+
| URL |
| PortalId |
| HTTPAlias |
| Page_Title |
| Report_Month |
| Report_Year |
| Pageviews |
| Unique_Pageviews |
| Entrances |
| Total_Time_on_Page |
| Bounces |
| Exits |
| Avg_Time_on_Page |
| Bounce_Rate |
| Exit_Rate |
+-----------------------+
我尝试使用IIF(Is Null(**)
,并且一直在寻找脚本本身,以了解为什么UNION
和JOIN
似乎不起作用,而且我似乎无法弄清楚出来。
这整个星期我一直在玩耍,只是没有来找我。
SELECT TOP 24 dbo_Good_URLs.Url, Nz(dbo_analytics_history.Pageviews, 0) AS Total_Pageviews,
Nz(dbo_analytics_history.Pageviews, 0) AS Month1
FROM (SELECT Url FROM dbo_Good_URLs WHERE HTTPAlias IN ('x.org', 'ab.x.org'))
UNION
SELECT Url FROM dbo_vw_GoodURLs WHERE dbo_Good_URLs.HTTPAlias IN ('x.org', 'ab.x.org')
LEFT OUTER JOIN dbo_analytics_history
ON dbo_Good_URLs.Url = dbo_analytics_history.URL AND dbo_analytics_history.HTTPAlias IN ('x.org', 'ab.x.org') AND dbo_analytics_history.Report_Month = 10
GROUP BY dbo_Good_URLs.Url, dbo_analytics_history.Pageviews
ORDER BY Nz(dbo_analytics_history.Pageviews, 0) DESC;
我要查找的结果是显示10月份(即第10个月)浏览的前24页
答案 0 :(得分:0)
我可能会猜测您实际上正在寻找以下内容:
select top 24 q.url, nz(a.pageviews, 0) as Total_Pageviews, nz(a.pageviews, 0) as Month1
from
(
select dbo_good_urls.url from dbo_good_urls
where dbo_good_urls.httpalias in ('x.org', 'ab.x.org')
union
select dbo_vw_goodurls.url from dbo_vw_goodurls
where dbo_vw_goodurls.httpalias in ('semcog.org', 'loggedin.semcog.org')
) q
left join dbo_analytics_history a on q.url = a.url
)
where
a.report_month is null or a.report_month = 10
order by
nz(a.pageviews, 0) desc;
在这里,目标URL是由两个联合的子查询选择的,这种联合的结果在dbo_analytics_history
字段上与您的url
表相连。