【问题标题】:SQL - How to find which page is the first for users?SQL - 如何找到哪个页面是用户的第一个页面?
【发布时间】:2017-12-12 22:37:38
【问题描述】:

我有一张这样的桌子:

+----------+-------------------------------------+----------------------------------+
| user_id  | time                                | url                              |
+----------+-------------------------------------+----------------------------------+
| 1        | 02.04.2017 8:56                     | www.landingpage.com/              | 
| 1        | 02.04.2017 8:57                     | www.landingpage.com/about-us     |  
| 1        | 02.04.2017 8:58                     | www.landingpage.com/faq          |
| 2        | 02.04.2017 6:34                     | www.landingpage.com/about-us     |
| 2        | 02.04.2017 6:35                     | www.landingpage.com/how-to-order |
| 3        | 03.04.2017 9:11                     | www.landingpage.com/             |
| 3        | 03.04.2017 9:12                     | www.landingpage.com/contact      |
| 3        | 03.04.2017 9:13                     | www.landingpage.com/about-us     |
| 3        | 03.04.2017 9:14                     | www.landingpage.com/our-legacy   |
| 3        | 03.04.2017 9:15                     | www.landingpage.com/             |
+----------+-------------------------------------+----------------------------------+

我想弄清楚哪个页面是大多数用户的第一页(用户访问网站时看到的第一页)并计算它被视为第一页的次数。

有没有办法编写查询来执行此操作?我想我需要使用

MIN(time)

结合分组,但我不知道如何。

所以关于我提供的样本应该是这样的:

url                                       url_count
---------------------------------------------------
www.landingpage.com/                      2            
www.landingpage.com/about-us              1            

谢谢!

【问题讨论】:

    标签: sql sql-server datetime group-by min


    【解决方案1】:

    您是对的,您需要在子选择中使用 min() 聚合函数。

    select
      my_table.url
    from
      my_table
    where
      my_table.time = (
        select
          min(t.time)
        from
          my_table t
        where
          t.user_id = my_table.user_id
      )
    

    my_table 替换为您的表的实际名称。


    要包含用户看过的页面数量,您需要这样的内容:

    select
      my_table.url
      , (
        select
          count(t.url)
        from
          my_table t
        where
          t.user_id = my_table.user_id
      ) as url_count
    from
      my_table
    where
      my_table.time = (
        select
          min(t.time)
        from
          my_table t
        where
          t.user_id = my_table.user_id
      )
    

    【讨论】:

    • 谢谢!如果我做对了,为了计算 url,我需要添加 url 分组,因此代码如下所示:select my_table.url, count(my_table.url) from my_table where my_table.time = ( select min(t.time) from my_table t where t.user_id = my_table.user_id ) group by my_table.url order by my_table.url desc 第一行将显示大多数用户最先使用的 url 和总浏览量。对吗?
    • @monsieurkovacs:不幸的是,没有。 where 子句将其限制为最小行。如果要包含计数,则需要在 select 子句中添加子选择。
    • @monsieurkovacs:我已经编辑了问题以包含用户数。请记住为有帮助的答案投票,并接受最有帮助的答案:)
    • 现在显示用户访问的入口页面和用户查看的页面总数。我需要的是计算入口页面的总数(请参阅更新的初始帖子)。我希望我现在已经足够清楚了。再次感谢! PS 我对每一个回复都投了赞成票,但由于我的声誉低于 15,所以没有显示投票
    【解决方案2】:
    SELECT * 
    FROM my_table
    WHERE time IN 
    (
      SELECT min(time) 
      FROM my_table
      GROUP BY url
    );
    

    【讨论】:

    • 如果您有两条具有相同 min [time] 记录的记录会怎样?
    • 提问者说“我需要使用MIN(time)”。答案是根据人的意愿写的。在您的条件下,时间值必须包含秒或毫秒值。我的意思是同一网址上不同用户的时间不应该相同。
    【解决方案3】:

    您可以如下查询:

    Select top (1) with ties * 
        from yourtable
    order by row_number() over(partition by user_id order by [time])
    

    您可以使用外部查询来获得与以下相同的结果:

    Select * from (
        Select *, RowN = row_number() over(partition by user_id order by [time]) from yourtable) a
    Where a.RowN = 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 2020-09-18
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多