未在2015年及其最后访问日期访问的患者

时间:2016-01-13 18:43:10

标签: mysql sql sql-server

以下是从2014/7/1到今天将未见供应商的患者拉到下一步的脚本。现在我想要一个新的专栏,我可以在他们最后一次遇到约会时如何做到这一点?下面显示了带有列的表。

如果您有任何疑问,请与我们联系。

SELECT p.first_name, p.last_name, p.date_of_birth, 
       p1.age, p.home_phone, p.cell_phone, 
       p.primarycareprovname, pe.enctimestamp, 
       pe.encnbr, p.city, p.zip, p.person_id    
FROM person AS p 
Inner join patient_ P1 
        on p.person_id=p1.person_id 
LEFT OUTER JOIN Patient_encounter PE 
        ON pe.person_id =p.person_id 
       AND pe.enctimestamp BETWEEN '20140701' AND '20160131'

WHERE pe.person_id IS NULL 
  and p1.age>'59' 
  and p.city like '%Paris%'

Click on this Link to see the table with columns

2 个答案:

答案 0 :(得分:0)

您可以使用GROUP BYMAX()

SELECT p.first_name, p.last_name, p.date_of_birth, p1.age, p.home_phone, 
       p.cell_phone, p.primarycareprovname,
       MAX(pe.enctimestamp) as most_recent_date, p.city, p.zip, p.person_id
FROM person AS p Inner join
     patient_ P1
     on p.person_id = p1.person_id LEFT OUTER JOIN
     Patient_encounter PE
      ON pe.person_id = p.person_id AND
         pe.enctimestamp BETWEEN '20140701' AND '20160131'
WHERE pe.person_id IS NULL and p1.age  >'59' and p.city like '%Paris%'
GROUP BY p.person_id;

答案 1 :(得分:0)

SELECT x.first_name, x.person_id, MAX(pe2.enctimestamp) as LastEncounterDate
FROM

(SELECT p.first_name, p.last_name, p.date_of_birth, 
       p1.age, p.home_phone, p.cell_phone, 
       p.primarycareprovname, pe.enctimestamp, 
       pe.encnbr, p.city, p.zip, p.person_id    
FROM person AS p 
Inner join patient_ P1 
        on p.person_id=p1.person_id 
LEFT OUTER JOIN Patient_encounter PE 
        ON pe.person_id =p.person_id 
       AND pe.enctimestamp BETWEEN '20140701' AND '20160131'
WHERE pe.person_id IS NULL 
  and p1.age>'59' 
  and p.city like '%Paris%') x

INNER JOIN Patient_encounter PE2 
    ON pe2.person_id =x.person_id
WHERE pe2.enctimestamp < '20140701'
GROUP BY x.first_name, x.person_id