按特定时间段拆分(如:按最近 2-3 个月...4-5 个月、6-7 个月拆分数据)

Split up by specific time period (Such as: split up data by last 2-3 months ...4-5 months, 6-7 months)

我有一个简单的问题,我不知道我哪里做错了。非常感谢你的帮助。预先感谢您的任何提示。

我有数据,需要将发布日期拆分为 1 个月、2-3 个月、4-5 个月、6-7 个月、7-9 个月等。 所以,我创建了一个查询;

Select    case
          when release_date >= current_date - interval '1 month' then 'Within 1 month'
          when release_date between (current_date - interval '2 months') and (current_date - interval '3 months') then '2-3 months'
          when release_date between (current_date - interval '4 months') and (current_date - interval '5 months') then '4-5 months'
          when release_date between (current_date - interval '6 months') and (current_date - interval '7 months') then '6-7 months'
          when release_date between (current_date - interval '8 months') and (current_date - interval '9 months') then '8-9 months'
          when release_date between (current_date - interval '10 months') and (current_date - interval '12 months') then '9-12 months'
          when release_date < current_date - interval '12 months' then '> 12 month'
          end as release_date,
          country,
          ....

最后我只能看到1个月内NULL>12个月.

如果我没看错您的查询,您需要交换“between”语句中的值:

Select    case
          when release_date >= current_date - interval '1 month' then 'Within 1 month'
          when release_date between (current_date - interval '3 months') and (current_date - interval '2 months') then '2-3 months'
          when release_date between (current_date - interval '5 months') and (current_date - interval '4 months') then '4-5 months'
          when release_date between (current_date - interval '7 months') and (current_date - interval '6 months') then '6-7 months'
          when release_date between (current_date - interval '9 months') and (current_date - interval '8 months') then '8-9 months'
          when release_date between (current_date - interval '12 months') and (current_date - interval '9 months') then '9-12 months'
          when release_date < current_date - interval '12 months' then '> 12 month'
          end as release_date,
          country,

我没有系统可以运行确认这一点,但它应该可以工作。

问题是 between 的工作方式,BETWEEN (lower_limit) AND (upper_limit)

我认为您的输出生成的计算不是“月”。答案取决于您使用的是哪个数据库管理系统。这是在 postgres 中执行的一种方法,但如果您使用的是另一个 dbms,那么您应该能够轻松更改 CTE 中计算月份的语法。使用 CTE 可以让您有一个干净的 case 语句。

    with calculated_months as
    (
      select release_date,  
      extract(year from age(current_date, release_date)) * 12
       + extract(month from age(current_date, release_date))
       + 1   as months_from_current
    from t1
    )
    select t.release_date, 
    case
        when c.months_from_current < 2 then 'Within 1 month'
        when c.months_from_current between 2 and 3 then '2-3 months'
        when c.months_from_current between 4 and 5 then '4-5 months'
        when c.months_from_current between 6 and 7 then '6-7 months'
        when c.months_from_current between 8 and 9 then '8-9 months'
        when c.months_from_current between 10 and 12 then '10-12 months'
        when c.months_from_current > 12 then '12+ months'
    end months_since_release,
    t.country
    from t1 t
    join calculated_months c
      on c.release_date = t.release_date

db-fiddle here

编辑:不确定您的数据如何显示,但您可能想在 CTE 中添加一个“不同的”(select 不同的发布日期...)。