SQL - 连接两个单独的 SQL 查询
我有一个表,用于存储 Web 应用程序上的页面点击量,存储
unique_row_id http_session_id page_name page_hit_timestamp
----------------------------------------------------------------
0 123456789 index.html 2010-01-20 15:00:00
1 123456789 info.html 2010-01-20 15:00:05
2 123456789 faq.html 2010-01-20 15:00:15
3 987654321 index.html 2010-01-20 16:00:00
4 987654321 faq.html 2010-01-20 16:00:05
5 987654321 info.html 2010-01-20 16:00:15
6 111111111 index.html 2010-01-20 16:01:00
7 111111111 faq.html 2010-01-20 16:01:05
8 111111111 info.html 2010-01-20 16:01:15
我想要运行的 sql 查询,该查询将向我显示用户最终浏览的最常见页面。
所以我最初的想法是,在我的(java)应用程序中,我可以运行一个查询,从表中选择不同的 http_session_id 值,然后对于每个不同的 http_session_id,运行另一个查询来获取具有“最新”page_hit_timestamp 的页面,并对所有这些页面的总数进行求和。 (对于上面的示例数据,info.html 的计数为 2,faq.html 的计数为 1。)
但是,我想知道的是:有没有办法将这两者结合起来查询单个 sql 语句 - 或者我必须沿着存储过程路线进行查询?
我看过使用 join,但我不知道它是否适用于这种情况。
PS - 我知道我可以在我的应用程序中使用 Google Analytics 之类的工具来为我提供此信息,但是 a) 这是一个移动网络应用程序,因此不太适合现成的分析工具,b) 我只是好奇知道这是否可以在 SQL 中完成。
I have a table that stores the page hits on a web application, storing
unique_row_id http_session_id page_name page_hit_timestamp
----------------------------------------------------------------
0 123456789 index.html 2010-01-20 15:00:00
1 123456789 info.html 2010-01-20 15:00:05
2 123456789 faq.html 2010-01-20 15:00:15
3 987654321 index.html 2010-01-20 16:00:00
4 987654321 faq.html 2010-01-20 16:00:05
5 987654321 info.html 2010-01-20 16:00:15
6 111111111 index.html 2010-01-20 16:01:00
7 111111111 faq.html 2010-01-20 16:01:05
8 111111111 info.html 2010-01-20 16:01:15
I want to run a sql query that will show me the most common page that users end browsing on.
So my initial thinking is that in my (java) app, I can run a query that will select the distinct http_session_id values from the table, and then for each distinct http_session_id, run another query that gets the page with the 'latest' page_hit_timestamp, and sum a total for of all these pages.
(For the sample data above, I'd have a count of 2 for info.html and a count of 1 for faq.html.)
But, what I'd like to know is this: is there a way to combine these two queries into a single sql statement - or would I have to go down the stored procedure route for that ?
I've had a look at using join, but I can't figure out if its applicable in this scenario.
PS - I know that I could use the likes of Google Analytics in my app to provide this info for me but a) this is a mobile web app so not great for off the shelf analytics tools, and b) I'm just curious to know if this can be done in SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该做你想要的:
This should do what you want:
这将为每个 http_session_id 和 page_name 组合返回一行,该行将包含:
This will return a row for each http_session_id and page_name combination, and that row will contain:
您能否提供您的两个查询,我可以轻松地将它们转换为 JOIN ,或者根据您的需要将它们转换为子查询。
Can you provide your two queries, I could turn them into a JOIN for you easily, or possibly a subquery depending upon your needs.
下面的查询列出了最近访问的页面,
如果您想要计数,那么下面的查询可能会有所帮助
The query below lists the last accessed pages,
if you want count, then the query below may help