【问题标题】:SQL Query Count Since Start of Quarter自季度开始以来的 SQL 查询计数
【发布时间】:2021-06-13 09:50:47
【问题描述】:

我正在尝试确定自季度开始以来查询数据集以仅计算个人一次的最佳方式。这是数据集:

Year     |    Qtr    |    Week    |    Name
2021          1           1            John Smith
2021          1           3            John Smith
2021          2           14           John Smith
2021          1           1            Julie Johnson
2021          1           2            Julie Johnson
2021          1           3            John Jacobs

我需要能够返回一个数据集,该数据集告诉我每周的唯一身份人数以及季度至今的唯一身份人数:

结果集:

Year     |    Qtr    |    Week    |    Week Count    |    Rolling QTR Count
2021          1           1            2                  2   
2021          1           2            1                  2
2021          1           3            2                  3
2021          2           14           1                  1

我曾尝试使用窗口函数进行计数,但我无法弄清楚如何仅在滚动时间范围内计算某人。

【问题讨论】:

  • 您使用的是哪个 dbms?
  • SQL Server 2012
  • 什么是滚动季度?您还说本季度至今。对我来说,这些是不同的事情 - 滚动季度要么是“过去 90 天,每天”,要么是“自 3 个月前的同一天开始,每天”(请注意,因为在月末跳跃,例如 2 月 28 日回到 11 月 28 日但 3 月 1 日然后跳过 11 月 29 日和 30 日),但 qtd 是“自本季度开始以来,每天”,这是一个长达 92 天的可变长度窗口

标签: sql sql-server sql-server-2012 window-functions rolling-computation


【解决方案1】:

只需在名称出现在每个季度的第一周添加一个标志,并将该标志用于季度列:

select year, qtr, week, count(*) as week_count,
       sum(sum(case when seqnum = 1 then 1 else 0 end)) over
           (partition by year, qtr order by week) as quarter_count
from (select d.*,
             row_number() over (partition by year, qtr, name order by week) as seqnum
      from dataset d
     ) d;

【讨论】:

    【解决方案2】:

    这里有一个解决您问题的方法(虽然我会尽量优化它):

    架构:

    create table mytable(Year int, Qtr int,Week int, Name varchar(50));
    insert into mytable values(2021,1,1,            'John Smith');
    insert into mytable values(2021,1,3 ,           'John Smith');
    insert into mytable values(2021,2,14 ,          'John Smith');
    insert into mytable values(2021,1,1   ,         'Julie Johnson');
    insert into mytable values(2021,1,2    ,        'Julie Johnson');
    insert into mytable values(2021,1,3     ,       'John Jacobs');
    

    查询:

    with RollingQtyCount as (
    select   m.year,m.qtr,m.week,count(name) over (partition by m.year,m.qtr,m.week) weekcount,
    count(t.distinct_names)over(partition by m.year,m.qtr order by m.week rows between unbounded preceding and current row) [Rolling QTR Count]from mytable m
    
    left join 
    (
         select year,qtr,week, name distinct_names from mytable m where 
    not exists (select name from mytable mt where m.name = mt.name and m.year = mt.year and m.qtr = mt.qtr and m.week > mt.week)
    
    ) t on m.year=t.year and m.qtr=t.qtr and m.week=t.week and m.name=t.distinct_names 
    )
    select year,qtr,week ,max(weekcount)weekcount,max([Rolling QTR Count]) [Rolling QTR Count] from RollingQtyCount
    group by year,qtr,week
    GO
    
    year qtr week weekcount Rolling QTR Count
    2021 1 1 2 2
    2021 1 2 1 2
    2021 1 3 2 3
    2021 2 14 1 1

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多