SQL - 连接两个单独的 SQL 查询

发布于 2024-08-18 18:01:49 字数 1230 浏览 8 评论 0原文

我有一个表,用于存储 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦情居士 2024-08-25 18:01:49

这应该做你想要的:

select 1.page_name, count(*) as ExitPageCount
from WebLog l
inner join (
    select http_session_id, max(page_hit_timestamp)
    from WebLog
    group by session
) lm on l.http_session_id = lm.http_session_id and l.page_hit_timestamp = lm.page_hit_timestamp
group by 1.page_name

This should do what you want:

select 1.page_name, count(*) as ExitPageCount
from WebLog l
inner join (
    select http_session_id, max(page_hit_timestamp)
    from WebLog
    group by session
) lm on l.http_session_id = lm.http_session_id and l.page_hit_timestamp = lm.page_hit_timestamp
group by 1.page_name
白云悠悠 2024-08-25 18:01:49
SELECT http_session_id, page_name, COUNT(page_name), MAX(page_hit_timestamp)
    FROM table
    GROUP BY http_session_id, page_name

这将为每个 http_session_id 和 page_name 组合返回一行,该行将包含:

  • http_session_id
  • page_name
  • (http_session_id+page_name) 组合在表中出现的次数
  • 该组合的最新 (MAX) 时间戳
SELECT http_session_id, page_name, COUNT(page_name), MAX(page_hit_timestamp)
    FROM table
    GROUP BY http_session_id, page_name

This will return a row for each http_session_id and page_name combination, and that row will contain:

  • http_session_id
  • page_name
  • the count of how many times the (http_session_id+page_name) combination occurs in the table
  • the latest (MAX) timestamp for the combination
窝囊感情。 2024-08-25 18:01:49

您能否提供您的两个查询,我可以轻松地将它们转换为 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.

冬天旳寂寞 2024-08-25 18:01:49

下面的查询列出了最近访问的页面,

select http_session_id,page_name,page_hit_timestamp from 
(select row_number() over( partition by t.http_session_id order by t.page_hit_timestamp desc) rn,t.* from weblog t
) where rn=1;

如果您想要计数,那么下面的查询可能会有所帮助

select page_name,count(*) from (select 
row_number() over( partition by t.http_session_id order by t.page_hit_timestamp desc) rn,t.* from weblog t
) where rn=1
group by page_name;

The query below lists the last accessed pages,

select http_session_id,page_name,page_hit_timestamp from 
(select row_number() over( partition by t.http_session_id order by t.page_hit_timestamp desc) rn,t.* from weblog t
) where rn=1;

if you want count, then the query below may help

select page_name,count(*) from (select 
row_number() over( partition by t.http_session_id order by t.page_hit_timestamp desc) rn,t.* from weblog t
) where rn=1
group by page_name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文