select ptn_id , study_date as st_date from study1 as a INNER JOIN 
patient1 as b ON a.ptn_id_id = b.ptn_id_id
where abs(datediff(day,study_date,getdate()))=
(select min(abs(datediff(day,study_date,getdate()))) from study1 where ptn_id=a.ptn_id)

解决方案 »

  1.   

    SELECT *
    FROM study1 a INNER JOIN 
    patient1 b ON a.ptn_id_id = b.ptn_id_id INNER JOIN
    series1 c ON a.study_uid_id = c.study_uid_id
    where study_date=
    (select max(study_date) from study1 where ptn_id=a.ptn_id)
      

  2.   

    楼上的代码不行的好伐,子查询里的ptn_id又不在study1表中
      

  3.   

    SELECT *
    FROM study1 a
    INNER JOIN patient1 b ON a.ptn_id_id = b.ptn_id_id 
    INNER JOIN series1 c ON a.study_uid_id = b.study_uid_id
    where not exists(select 1 from study1 
                     where ptn_id_id=a.ptn_id_id 
                     and study_date>a.study_date)
      

  4.   

    SELECT *
    FROM study1 a INNER JOIN 
    patient1 b ON a.ptn_id_id = b.ptn_id_id INNER JOIN
    series1 c ON a.study_uid_id = c.study_uid_id
    where study_date=
    (select max(study_date) from study1 where ptn_id=a.ptn_id)
      

  5.   

    那就改一下吧
    SELECT *
    FROM study1 a INNER JOIN 
    patient1 b ON a.ptn_id_id = b.ptn_id_id INNER JOIN
    series1 c ON a.study_uid_id = c.study_uid_id
    where study_date=
    (select max(study_date) from study1 where ptn_id=b.ptn_id)