我需要统计 start_date 后 30 天内的记录?
I Need to count records in next 30 days from start_date?
我正在尝试查找每条记录开始日期后 30 天内的记录数
我有一个 table:
Patid Start_date
1234 1/1/2015
1234 1/10/2015
1234 1/30/2015
1234 2/19/2015
1234 3/5/2015
1234 3/6/2015
1234 3/7/2015
我想编写一个简单的 sql 查询,应该会得到以下结果:
patid: Start_Date #of Records in Next 30 Days
1234 1/1/2015 2
1234 1/10/2015 2
1234 1/30/2015 1
1234 2/19/2015 3
1234 3/5/2015 2
1234 3/6/2015 1
1234 3/7/2015 0
此致,
晴天
在通用 SQL 中,最简单的方法是使用相关子查询:
select t.*,
(select count(*)
from table t2
where t2.patid = t.patid and
t2.start_date > t.start_date and
t2.start_date <= t.start_date + interval '30 days'
) as Next30Days
from table t;
这对日期算法使用了 ANSI 标准语法——该标准主要在违规行为中得到遵守。每个数据库似乎都有自己的日期处理规则。
我正在尝试查找每条记录开始日期后 30 天内的记录数
我有一个 table:
Patid Start_date
1234 1/1/2015
1234 1/10/2015
1234 1/30/2015
1234 2/19/2015
1234 3/5/2015
1234 3/6/2015
1234 3/7/2015
我想编写一个简单的 sql 查询,应该会得到以下结果:
patid: Start_Date #of Records in Next 30 Days
1234 1/1/2015 2
1234 1/10/2015 2
1234 1/30/2015 1
1234 2/19/2015 3
1234 3/5/2015 2
1234 3/6/2015 1
1234 3/7/2015 0
此致, 晴天
在通用 SQL 中,最简单的方法是使用相关子查询:
select t.*,
(select count(*)
from table t2
where t2.patid = t.patid and
t2.start_date > t.start_date and
t2.start_date <= t.start_date + interval '30 days'
) as Next30Days
from table t;
这对日期算法使用了 ANSI 标准语法——该标准主要在违规行为中得到遵守。每个数据库似乎都有自己的日期处理规则。