【问题标题】:Calculating time gaps between appointment dates for a patient in clinic in mySQL在 mySQL 中计算诊所患者的预约日期之间的时间间隔
【发布时间】:2014-11-12 08:15:55
【问题描述】:

我想从数据库中计算出患者是否迟到了他们的预约,以及迟到了多少。

我有一个来自诊所的数据表,其中包含列:

  1. Pt - 患者编号
  2. Appt - 患者就诊的日期
  3. Next_appt - 指定的下一个约会日期

看起来像这样:

Pt  Appt        Next_appt
12  2013-04-22  2013-05-21
12  2013-05-20  2013-07-15
57  2010-06-08  2010-07-05
57  2010-08-03  2010-10-19
127 2009-02-24  2009-06-23
127 2009-04-20  2009-05-11

因此对于患者 12 - 他被分配了 next_appt 日期为 2013 年 5 月 21 日,他参加了 2013 年 5 月 20 日(提前 1 天)

将 Next_appt 列向下移动 1 列然后减去这 2 列很容易,但那样我会遇到麻烦,因为那时日期将对应于不同的患者。

数据库中有大约 20,000 名患者(!)。

不知道有没有人可以给我一些建议?

谢谢,

【问题讨论】:

  • 我不能 100% 确定您的描述。你能添加“预期结果”吗?

标签: mysql datediff


【解决方案1】:

试试这个:

select
    t1.*,
    case sign(t2.appt - t1.next_appt)
         when 0 then 'on time'
         when 1 then 'late'
         when -1 then 'early' end 
from 
    your_table t1
left join
    your_table t2
on 
    t1.pt = t2.pt -- shift down with pt as join condition
and t1.appt < t2.appt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 2021-04-30
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多