Sql 添加下一个约会日期

Sql 添加下一个约会日期,sql,sql-server,tsql,reporting-services,report,Sql,Sql Server,Tsql,Reporting Services,Report,我有一份召回报告,根据召回计划为每个需要在特定日期返回办公室的患者出具信函。召回计划根据患者的病情确定患者何时返回办公室。我还有一张表,它存储了每个患者过去和现在的所有预约。“召回计划”表中的约会是自动生成的,而“约会”表中的约会是手动创建的。如果有人打电话预约,则不会检查召回计划报告,因为两个表中经常出现相同的预约,导致发出重复的提醒信 我需要做两件事: 我知道我的方法不一定能解决业务问题,但这就是我的任务 我需要制作一份清单,列出 每位患者的下一次预约 但只有在未来。 我需要在第一列中添加一

我有一份召回报告,根据召回计划为每个需要在特定日期返回办公室的患者出具信函。召回计划根据患者的病情确定患者何时返回办公室。我还有一张表,它存储了每个患者过去和现在的所有预约。“召回计划”表中的约会是自动生成的,而“约会”表中的约会是手动创建的。如果有人打电话预约,则不会检查召回计划报告,因为两个表中经常出现相同的预约,导致发出重复的提醒信

我需要做两件事: 我知道我的方法不一定能解决业务问题,但这就是我的任务

我需要制作一份清单,列出 每位患者的下一次预约 但只有在未来。 我需要在第一列中添加一列 报告显示每个患者的下一个症状 预约,以便有人可以手动 识别重复的字母 会为特定的病人外出 并据此进行干预。 召回报告查询:

SELECT description as [Plan Name], 
per.first_name + ' ' + per.last_name as [Patient],
substring (plan_start_date, 5,2) + '-' +
substring (plan_start_date, 7,2) + '-' + 
substring (plan_start_date, 1,4) as [Plan Start Date],
substring (nr.expected_return_date, 5,2) + '-' +
substring (nr.expected_return_date, 7,2) + '-' + 
substring (nr.expected_return_date, 1,4) as [Expected Return Date] 
FROM recall_plan_mstr rp, 
patient_recall_plans nr, 
patient pt, 
person per
WHERE rp.practice_id = nr.practice_id 
and rp.recall_plan_id = nr.recall_plan_id 
and nr.practice_id = pt.practice_id 
and nr.person_id = pt.person_id 
and per.person_id = pt.person_id 
and (active_plan_ind = 'Y') 
and rp.practice_id = '0025' 
召回报告结果:

PLAN NAME          PATIENT          START      RETURN
------------------ ---------------- ---------- ----------
OFFICE VISIT W/ DR Charles Span     04-18-2011 12-15-2011
LIPID PANEL        Ronald Chap      04-11-2011 06-28-2011
OFFICE VISIT W/ DR Ronald Chap      04-11-2011 04-21-2011
OFFICE VISIT W/ DR Will Thor        03-31-2011 02-01-2012
PACEMAKER CHECK    Sylvia Berkly    05-03-2011 08-03-2011
OFFICE VISIT W/ DR Tim Cayle        04-13-2011 09-26-2011
OFFICE VISIT W/ DR Caferana Mercade 04-11-2011 10-08-2011
OFFICE VISIT W/ DR Susanna Calter   05-10-2011 05-07-2012
ICD CHECK          Jim Southern     04-14-2011 07-13-2011
STRESS ECHO        Don Cobey        04-28-2011 06-07-2010
预约查询:

select person_id, appt_date
from appointments 
where person_id is not null 
group by person_id, appt_date
order by person_id, appt_date desc
任命结果:

person_id                            appt_date
------------------------------------ ---------
073C8F83-CE15-4192-8E12-00006CB5A433 20091228
073C8F83-CE15-4192-8E12-00006CB5A433 20090510
073C8F83-CE15-4192-8E12-00006CB5A433 20090301
073C8F83-CE15-4192-8E12-00006CB5A433 20081006
378A281C-FAE7-43DF-BC03-00006E386680 20110509
378A281C-FAE7-43DF-BC03-00006E386680 20110217
378A281C-FAE7-43DF-BC03-00006E386680 20110124
378A281C-FAE7-43DF-BC03-00006E386680 20110111
378A281C-FAE7-43DF-BC03-00006E386680 20101207
816D4D31-3C99-4762-878D-000097883B73 20110316
816D4D31-3C99-4762-878D-000097883B73 20101216
问题:

如何从列表中生成列表 结果为的约会表 每行一名患者,仅限 最近一次约会是在 将来我需要写一个游标吗 为了这个? 我怎样才能把这张单子混合到我的工作中去呢 召回报告,使其有一列 返回列的右侧 显示患者的下一个目标 预约日期仅限未来?二者都 表具有个人编号GUID。
我希望我已经充分解释并提供了足够的信息。如果需要任何其他信息,请随时询问。

第一个问题的答案如下:

SELECT person_id, min(appt_date) as appt_date
FROM appointments 
WHERE person_id is not null
   AND now() < appt_date -- This line is database specific
GROUP BY person_id
ORDER BY person_id
第二个问题的答案是,您需要一个左连接。如果在任何地方都使用联接语法,则左联接更容易。这是问题所在

SELECT description as [Plan Name]
  , per.first_name + ' ' + per.last_name as [Patient]
  , substring (plan_start_date, 5,2) + '-' +
    substring (plan_start_date, 7,2) + '-' + 
    substring (plan_start_date, 1,4) as [Plan Start Date]
  , substring (nr.expected_return_date, 5,2) + '-' +
    substring (nr.expected_return_date, 7,2) + '-' + 
    substring (nr.expected_return_date, 1,4) as [Expected Return Date]
  , CASE
      WHEN next_appt.appt_date IS NULL
      THEN ''
      ELSE substring (next_appt.appt_date, 5,2) + '-' +
        substring (next_appt.appt_date, 7,2) + '-' + 
        substring (next_appt.appt_date, 1,4)
    END as [Next Appointment Date]
FROM recall_plan_mstr rp
  JOIN patient_recall_plans nr
    ON rp.recall_plan_id = nr.recall_plan_id
  JOIN patient pt
    ON nr.practice_id = pt.practice_id
      AND nr.person_id = pt.person_id 
  JOIN person per
    ON and per.person_id = pt.person_id
  LEFT JOIN (
        SELECT person_id, min(appt_date) as appt_date
        FROM appointments 
        WHERE person_id is not null
          AND now() < appt_date -- This line is database specific
        GROUP BY person_id
        ORDER BY person_id
      ) next_appt
    ON next_appt.person_id = pt.person_id
WHERE (active_plan_ind = 'Y') 
  AND rp.practice_id = '0025'
您会注意到,我对查询进行了格式化以确保其易读性。请参阅,以了解我为什么选择使用这种方式格式化SQL的说明