【问题标题】:SQL last 6 months visitsSQL 最近 6 个月的访问量
【发布时间】:2021-09-08 10:35:28
【问题描述】:

报告目的:确定过去 6 个月内未进行牙齿清洁的患者

编写 sql 脚本的最佳方法是什么?

患者表

patient_id patient_name
11 Jason Strong
22 Ryan Smith
33 Casey Hammer

访问表

v_id patient_id reason_visit date_of_visit
1 11 medical 01/01/2021
2 22 dental cleaning 11/10/2020
3 22 annual 01/01/2021
4 11 dental cleaning 5/10/2021
5 11 annual 5/1/2021

预期

patient_id patient_name
22 Ryan Smith
33 Casey Hammer

Casey 在名单上,因为她不在访问表中,这意味着她从未收到我们办公室的清洁服务。

Ryan Smith 在名单上是因为该打扫卫生了。

我还在考虑如果患者在过去 6 个月内没有预约但未来预约了牙齿清洁,该怎么办。我想排除它。

【问题讨论】:

  • 标记你的数据库
  • 将有助于分享您尝试解决的代码以获得一些帮助

标签: sql aggregates


【解决方案1】:

在postgresql中:

select * from Patients p
where not exists (
    select 1 from Visits v
    where v.patient_id = p.patient_id
    and reason_visit = 'dental cleaning'
    and date_of_visit < now() - interval '6 month'
)

在 sql server 中将 now() - interval '6 month' 替换为 dateadd(month, -6,getdate())

在mysql中date_add(now(), interval -6 month)

【讨论】:

  • 作者应该发布一些他们尝试解决 imo 的努力
猜你喜欢
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 2016-03-28
  • 1970-01-01
  • 2017-06-17
  • 2021-12-01
  • 1970-01-01
  • 2019-12-02
相关资源
最近更新 更多