如何创建具有日期间隙的 SQL 组
我有以下查询:
SELECT patient_id FROM patient_visit where visit_type in ('A', 'B', 'C')
group by patient_id having count(*) >= 2
获取至少两次就诊的所有患者的列表,类型为“A”、“B”或“C”。
Patient_visit 表还有一个 Visit_date 列,用于存储就诊日期。我的问题:是否可以修改上述查询而不删除 group by 语句来查询“所有至少两次就诊的患者并且其中任何两次就诊的差距为 60 天数”?
谢谢!
PS:我使用的是Oracle,如果有内置函数,我也可以使用它。
I have the following query:
SELECT patient_id FROM patient_visit where visit_type in ('A', 'B', 'C')
group by patient_id having count(*) >= 2
To get a list of all patients that had at least two visits of type 'A', 'B', or 'C'.
The patient_visit table also has a visit_date column which stores the date of the visit. My question: is it possible to modify the above query WITHOUT removing the group by statement to query "all patients with at least two visits AND where any of those two visit had a gap of 60
number of days"?
Thanks!
P.S.: i'm using Oracle, if there's a built-in function, I can use that too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
任意两个日期,第一次和最后一次访问都符合条件吗?
如果您的意思是连续的,那么
这是一个昂贵的查询,其数量级为 O(N3)。 Oracle LAG 版本可能会更快。
Any two dates, so the first and last visits would qualify?
If you meant consecutive, then
This is an expensive query, something of the order O(N3). The Oracle LAG version could be faster.
我没有要测试的预言机,但我认为这会起作用
I dont have oracle to test but I think this will work