【问题标题】:sql divide time using between in case whensql 使用 between 来划分时间以防万一
【发布时间】:2021-10-30 01:01:01
【问题描述】:

我有一个数据集 退出表

exit_id | clicktime
123     | 15:46:41
424     | 12:43:22
343     | 2:44:33
543     | 20:44:33

将点击时间分成几部分

[12am - 6am]

[早上 6 点 - 早上 9 点]

[上午 9 点 - 下午 12 点]

[中午 12 点 - 下午 3 点]

[下午 3 点 - 下午 6 点]

[下午 6 点 - 晚上 9 点]

[晚上 9 点 - 凌晨 12 点]

期望的输出

exit_id | Most_Active_Time
123     | 3pm - 6pm
424     | 12pm - 3pm
343     | 12am - 6am
543     | 6pm - 9pm

【问题讨论】:

  • 你试过什么? case 表达式并不复杂。另外,标记您正在使用的数据库。
  • 我无法解决你能帮我查询一下吗
  • 我只尝试案例
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: sql group-by case


【解决方案1】:
with
w_query as (
    select 123 as exit_id, '15:16:41'::time as click_time
    union
    select 424, '12:43:22'::time
    union
    select 343, '2:44:33'::time
    union
    select 543, '20:44:33'::time
)
select
    exit_id,
    case
        when click_time between '15:00:00'::time and '18:59:59'::time then '3pm - 6pm'
        when click_time between '12:00:00'::time and '15:59:59'::time then '12pm - 3pm'
        else 'null'
    end
from w_query

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多