【问题标题】:SQL - Count values in a column using criteria and partition bySQL - 使用标准和分区计算列中的值
【发布时间】:2020-12-30 07:45:46
【问题描述】:

我有以下数据集,其中包含动物及其疫苗接种日期。我试图通过 SQL 在每条记录中计算宠物在过去 90、180 和 365 天内接种了多少疫苗。我可以在 Excel 中解决这个问题。将下表粘贴到 Excel 单元格 A1 中,并将以下公式 COUNTIFS(C:C,"<"&C2,C:C,">="&C2-90,A:A,A2) 放入单元格 D2 中。您可以将90分别调整为180365

Animal  Visit_ID    Vaccine_Date    Count_90    Count_180   Count_365
Cat 1   7/22/2017   0   0   0
Cat 2   8/1/2017    1   1   1
Cat 3   8/14/2017   2   2   2
Cat 4   8/23/2017   3   3   3
Cat 5   9/11/2017   4   4   4
Cat 6   9/30/2017   5   5   5
Cat 7   10/11/2017  6   6   6
Cat 8   10/23/2017  6   7   7
Cat 9   10/31/2017  6   8   8
Cat 10  11/6/2017   7   9   9
Cat 11  11/17/2017  7   10  10
Cat 12  11/29/2017  7   11  11
Cat 13  12/11/2017  7   12  12
Cat 14  12/25/2017  8   13  13
Cat 15  1/2/2018    8   14  14
Cat 16  1/29/2018   7   13  15
Cat 17  2/22/2018   5   12  16
Cat 18  3/9/2018    5   13  17
Cat 19  3/21/2018   5   13  18
Cat 20  4/13/2018   4   12  19
Cat 21  5/21/2018   4   9   20
Cat 22  8/27/2018   0   4   17
Cat 23  9/18/2018   1   3   17
Cat 24  10/3/2018   2   4   17
Cat 25  12/19/2018  1   3   11
Cat 26  12/22/2018  2   4   12
Cat 27  1/6/2019    2   5   11
Cat 28  1/30/2019   3   6   11
Cat 29  3/10/2019   4   6   10
Cat 30  3/26/2019   3   6   10
Cat 31  4/17/2019   3   6   10
Cat 32  5/13/2019   3   7   11
Cat 33  5/18/2019   4   8   12
Cat 34  5/25/2019   5   9   12
Cat 35  6/17/2019   5   10  13
Cat 36  7/2/2019    5   9   14
Cat 37  7/12/2019   6   9   15
Cat 38  8/2/2019    6   9   16
Cat 39  8/15/2019   6   10  17
Cat 40  8/27/2019   5   11  18
Cat 41  9/9/2019    6   11  18
Cat 42  9/17/2019   6   12  19
Cat 43  9/26/2019   7   12  19
Cat 44  10/9/2019   7   13  19
Cat 45  10/19/2019  7   13  20
Cat 46  11/12/2019  7   13  21
Cat 47  11/15/2019  7   13  22
Cat 48  11/26/2019  7   13  23
Cat 49  12/20/2019  6   13  23
Cat 50  12/31/2019  6   13  23
Cat 51  2/14/2020   3   11  22
Cat 52  3/8/2020    3   10  23
Cat 53  4/6/2020    2   9   22
Cat 54  5/5/2020    3   8   22
Cat 55  5/23/2020   3   7   21
Cat 56  6/18/2020   3   6   20
Cat 57  6/30/2020   4   6   21
Cat 58  7/16/2020   4   7   20
Cat 59  7/22/2020   5   8   21
Dog 1   3/8/2018 0:00   0   0   0
Dog 2   4/18/2019 0:00  0   0   0
Dog 3   7/1/2019 0:00   1   1   1
Dog 4   12/12/2019 0:00 0   1   2
Dog 5   12/23/2019 0:00 1   2   3

但是,当我尝试使用以下代码通过 SQL 完成此操作时,它只是查看前一行并添加。它似乎不像上面的 Excel 公式那样在每个疫苗日期倒数,而且我不确定如何集成一个窗口函数来计算过去发生的疫苗日期,基于 90,180,365 个间隔,同时被动物划分。

select
qry3.Animal
,qry3.Vaccine_Date
,(case when qry3.Count_90 = 0 then 0 else row_number() over (partition by qry3.Animal, qry3.Count_90_2 order by qry3.animal_rank) - 1 end) as Admit_90
,(case when qry3.Count_180 = 0 then 0 else row_number() over (partition by qry3.Animal, qry3.Count_180_2 order by qry3.animal_rank) - 1 end) as Admit_180
,(case when qry3.Count_365 = 0 then 0 else row_number() over (partition by qry3.Animal, qry3.Count_365_2 order by qry3.animal_rank) - 1 end) as Admit_365 
from

(
select
qry2.Animal
,qry2.Vaccine_Date
,qry2.animal_rank
,qry2.Count_90
,qry2.Count_180
,qry2.Count_365
,sum(case when qry2.Count_90 = 0 then 1 else 0 end) over(partition by qry2.Animal order by qry2.animal_rank rows between unbounded preceding and current row) as Count_90_2
,sum(case when qry2.Count_180 = 0 then 1 else 0 end) over(partition by qry2.Animal order by qry2.animal_rank rows between unbounded preceding and current row) as Count_180_2
,sum(case when qry2.Count_365 = 0 then 1 else 0 end) over(partition by qry2.Animal order by qry2.animal_rank rows between unbounded preceding and current row) as Count_365_2

from
(
select
qry1.Animal
,qry1.Vaccine_Date
,qry1.animal_Rank
,case when qry1.Vaccine_Date-qry1.Previous_Vaccine_Date < 90 then 1 else 0 end as Count_90
,case when qry1.Vaccine_Date-qry1.Previous_Vaccine_Date < 180 then 1 else 0 end as Count_180
,case when qry1.Vaccine_Date-qry1.Previous_Vaccine_Date < 365 then 1 else 0 end as Count_365

from
(
select
a.Animal
,a.Vaccine_Date
,b.Vaccine_Date as Previous_Vaccine_Date
,row_number() over (partition by null order by A.Animal,a.Vaccine_Date) as animal_Rank

from Animal_Vaccine a
left join Animal_Vaccine b on a.Visit_ID = b.Visit_ID - 1

) as qry1
) as qry2
) as qry3

以下是 SQL 的结果(我再次尝试模仿顶部的 excel 公式):

Animal  Vaccine_Date    Count_90    Count_180   Count_365
Cat 7/22/2017 0:00  0   0   0
Cat 8/1/2017 0:00   1   1   1
Cat 8/14/2017 0:00  2   2   2
Cat 8/23/2017 0:00  3   3   3
Cat 9/11/2017 0:00  4   4   4
Cat 9/30/2017 0:00  5   5   5
Cat 10/11/2017 0:00 6   6   6
Cat 10/23/2017 0:00 7   7   7
Cat 10/31/2017 0:00 8   8   8
Cat 11/6/2017 0:00  9   9   9
Cat 11/17/2017 0:00 10  10  10
Cat 11/29/2017 0:00 11  11  11
Cat 12/11/2017 0:00 12  12  12
Cat 12/25/2017 0:00 13  13  13
Cat 1/2/2018 0:00   14  14  14
Cat 1/29/2018 0:00  15  15  15
Cat 2/22/2018 0:00  16  16  16
Cat 3/9/2018 0:00   17  17  17
Cat 3/21/2018 0:00  18  18  18
Cat 4/13/2018 0:00  19  19  19
Cat 5/21/2018 0:00  20  20  20
Cat 8/27/2018 0:00  21  21  21
Cat 9/18/2018 0:00  22  22  22
Cat 10/3/2018 0:00  23  23  23
Cat 12/19/2018 0:00 24  24  24
Cat 12/22/2018 0:00 25  25  25
Cat 1/6/2019 0:00   26  26  26
Cat 1/30/2019 0:00  27  27  27
Cat 3/10/2019 0:00  28  28  28
Cat 3/26/2019 0:00  29  29  29
Cat 4/17/2019 0:00  30  30  30
Cat 5/13/2019 0:00  31  31  31
Cat 5/18/2019 0:00  32  32  32
Cat 5/25/2019 0:00  33  33  33
Cat 6/17/2019 0:00  34  34  34
Cat 7/2/2019 0:00   35  35  35
Cat 7/12/2019 0:00  36  36  36
Cat 8/2/2019 0:00   37  37  37
Cat 8/15/2019 0:00  38  38  38
Cat 8/27/2019 0:00  39  39  39
Cat 9/9/2019 0:00   40  40  40
Cat 9/17/2019 0:00  41  41  41
Cat 9/26/2019 0:00  42  42  42
Cat 10/9/2019 0:00  43  43  43
Cat 10/19/2019 0:00 44  44  44
Cat 11/12/2019 0:00 45  45  45
Cat 11/15/2019 0:00 46  46  46
Cat 11/26/2019 0:00 47  47  47
Cat 12/20/2019 0:00 48  48  48
Cat 12/31/2019 0:00 49  49  49
Cat 2/14/2020 0:00  50  50  50
Cat 3/8/2020 0:00   51  51  51
Cat 4/6/2020 0:00   52  52  52
Cat 5/5/2020 0:00   53  53  53
Cat 5/23/2020 0:00  54  54  54
Cat 6/18/2020 0:00  55  55  55
Cat 6/30/2020 0:00  56  56  56
Cat 7/16/2020 0:00  57  57  57
Cat 7/22/2020 0:00  58  58  58
Dog 3/8/2018 0:00   0   0   0
Dog 4/18/2019 0:00  0   0   0
Dog 7/1/2019 0:00   1   1   1
Dog 12/12/2019 0:00 0   2   2
Dog 12/23/2019 0:00 1   3   3

【问题讨论】:

  • 您的 Teradata 版本是什么?
  • 看起来我使用的是 15.10 版

标签: sql datetime count teradata window-functions


【解决方案1】:

我能够通过在动物身上和疫苗日期之间加入自己的表格,然后计算不同的 visit_id 来解决这个问题。我完全把它复杂化了,但我喜欢它,但会打开一个窗口函数!

select
a.Animal
,a.Vaccine_Date
,a.visit_id
,count(distinct b.visit_id) - 1 as Count_90 --minus 1 so it as to not count itself
,count(distinct c.visit_id) - 1 as Count_180 --minus 1 so it as to not count itself
,count(distinct d.visit_id) - 1 as Count_365 --minus 1 so it as to not count itself

from Animal_Vaccine a
left join Animal_Vaccine b on a.Animal = b.Animal and b.Vaccine_date between a.Vaccine_Date - 90 and a.Vaccine_Date
left join Animal_Vaccine c on a.Animal = c.Animal and c.Vaccine_date between a.Vaccine_Date - 180 and a.Vaccine_Date
left join Animal_Vaccine d on a.Animal = d.Animal and d.Vaccine_date between a.Vaccine_Date - 365 and a.Vaccine_Date

group by 1,2,3
;

【讨论】:

  • 每个Animal 的行数似乎很少,否则这个三重乘积连接会消耗大量 CPU :-)
【解决方案2】:

不幸的是,Teradata 不支持范围窗口框架,因此您无法使用窗口函数做您想做的事情。 dnoeth 建议的使用左连接和条件聚合的解决方案提供了一种有效的解决方法。

然而,我想知道几个相关的子查询是否会表现得更好,因为它根本不需要外部聚合:

select
    v.*,
    (select count(*) from vt v1 where v1.animal = v.animal and v1.vaccine_date >= v.vaccine_date -  90 and v1.vaccine_date < v.vaccine_date) count_90,
    (select count(*) from vt v1 where v1.animal = v.animal and v1.vaccine_date >= v.vaccine_date - 180 and v1.vaccine_date < v.vaccine_date) count_180,
    (select count(*) from vt v1 where v1.animal = v.animal and v1.vaccine_date >= v.vaccine_date - 365 and v1.vaccine_date < v.vaccine_date) count_365
from vt v

对于此查询,请确保您在 (animal, vaccine_date) 上有一个索引。

【讨论】:

  • 应该比这三个 join plus distinct 更好,但由于 Teradata 是一个庞大的并行系统,那些不等式的标量相关子查询也很难优化。除非它是物化视图,否则该索引将无济于事,并且可能不允许他在该表上创建索引。
【解决方案3】:

只要每个值的行数很低,你的方法就可以了。但是如果每个值的行数增加,CPU 会爆炸,你的 DBA 会打电话给你:-)

但您的查询查询可以简化为单个产品连接,而不是三个,假设 animal/visit_id 是唯一的:

select
   a.Animal
  ,a.Vaccine_Date
  ,a.visit_id
  ,count(case when b.Vaccine_date between a.Vaccine_Date - 90 and a.Vaccine_Date then b.visit_id end)  as Count_90
  ,count(case when b.Vaccine_date between a.Vaccine_Date - 180 and a.Vaccine_Date then b.visit_id end) as Count_180
  ,count(b.visit_id) as Count_365
from vt a
left join vt b
  on a.Animal = b.Animal
 and b.Vaccine_date between a.Vaccine_Date - 365 and a.Vaccine_Date - 1 -- don't include current row
group by 1,2,3

如果Animal 是主索引,则可以提高性能(否则也可以使用该索引创建易失性表)。

根据每只动物的行数和日期范围,最好使用 EXPAND 简单地创建缺失的日期,然后可以使用 ROWS 代替 RANGE:

select animal, Visit_ID, Vaccine_date
   -- EXPAND ON returned one row per day with repeated data, the CASE effectily NULLs the added rows
  ,count(case when valid_from = Vaccine_date then 1 end) over (partition by animal order by Vaccine_date rows between 90 preceding and 1 preceding)
  ,count(case when valid_from = Vaccine_date then 1 end) over (partition by animal order by Vaccine_date rows between 180 preceding and 1 preceding)
  ,count(case when valid_from = Vaccine_date then 1 end) over (partition by animal order by Vaccine_date rows between 365 preceding and 1 preceding)
from
 ( -- create the missing dates to get 1 row per animal/day
   select animal, Visit_ID, begin(pd) as Vaccine_date, valid_from
   from
    ( 
      select animal, Visit_ID
        ,cast(Vaccine_date as date) as valid_from -- seems to be a Timestamp
        -- get the next row's Vaccine_date for EXPAND ON in following step
/*      -- LAG/LEAD not supported in TD 15.10
        ,lead(Vaccine_date,1,Vaccine_date+1)
         over (partition by animal
               order by Vaccine_date) as valid_to 
 */     -- workaround for LEAD
        ,coalesce(min(Vaccine_date)
                  over (partition by animal
                        order by Vaccine_date
                        rows between 1 following and 1 following)
                 ,Vaccine_date +1) as valid_to -- replace the last row's NULL with a valid end date 
      from vt
    ) as prepare_data
   expand on period(valid_from, valid_to) as pd
 )  as expand_data
-- now remove the added dates again
qualify valid_from = Vaccine_date

这假设每只动物每天只有一排,否则您将在 EXPAND ON 期间收到错误消息。然后必须在准备步骤中汇总数据。

【讨论】:

    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 2016-09-24
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多