【发布时间】: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分别调整为180和365。
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