【问题标题】:Usage of two different timestamps in one SQL query在一个 SQL 查询中使用两个不同的时间戳
【发布时间】:2020-08-06 15:04:59
【问题描述】:

我遇到了一个 SQL 挑战: 2019年每天页面访问量和注册量

数据集如下所示:

我不明白如何计算每天的页面访问次数和每天的注册次数,因为它们基于两个不同的时间戳(users.user_registration_timestamp 和 informations.info_timestamp)?我需要使用子查询吗? SQL 查询会是什么样子?

【问题讨论】:

  • 乍一看你可以加入两个表并有一个WHERE条件来获取所需日期内的相关记录然后你可以COUNT你想要什么(你可能需要使用@ 987654325@ 虽然)
  • 独立的列无关紧要。价值确实如此。 “一天”是任何时间戳的组成部分,无论它存储在哪个字段中。提供实际挑战要求可能会增加清晰度。

标签: sql timestamp


【解决方案1】:

你不需要join,你只需要单独的Selects:

select cast(user_registration_timestamp as date), count(*)
from users
where user_registration_timestamp >= timestamp '2019-00-01 00:00:00'
  and user_registration_timestamp < timestamp '2020-00-01 00:00:00'
group by cast(user_registration_timestamp as date)

union all

select cast(info_timestamp as date), count(*)
from informations
where info_timestamp >= timestamp '2019-00-01 00:00:00'
  and info_timestamp < timestamp '2020-00-01 00:00:00'
group by cast(info_timestamp as date)

这将返回两行,如果你想要一个单行 CROSS JOIN 两个 Selects。

【讨论】:

    猜你喜欢
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 2021-03-31
    相关资源
    最近更新 更多