连接多个表后删除重复的行

时间:2015-06-20 01:00:10

标签: sql sql-server tsql

我有一个查询可以消除重复项,只显示唯一记录。

SELECT DISTINCT TBLPATIENT.MRN
FROM tblpatient 

输出:

MRN      
------
15257     
15283
15285    
15290     
15291      
15302

但是,我需要显示其他列,但我只想要唯一的MRN

SELECT 
      v.PatientID,
      p.firstname, 
      p.lastname, 
      p.dob, 
      p.mrn, 
      s.VisitID,
      v.AdmitedDate
 FROM
     tblPatient p
 JOIN 
     tblPatientVisit v ON p.id = v.PatientID
 JOIN 
     tblPatientSmokingScreenOrder s ON v.id = s.VisitID
 JOIN 
     DESCRIPTOR t ON s.SMOKING_STATUS_DSC_ID = DESCRIPTOR_ID
 WHERE 
      isdate(p.DOB) = 1 
      AND CONVERT(date,p.DOB) <'12/10/2000' 
      AND v.PatientType = 'I' and 
      isdate(v.AdmitedDate) = 1 
      AND CONVERT(date,v.AdmitedDate) > '06/16/2013 16:16'

输出:

PatientID   firstname   lastname    dob         mrn     VisitID AdmitedDate 
---------------------------------------------------------------------------
1           JAMES       TEST        6/11/1942   100241  1       54:00.0 
10          TEST3       DEMOGRAPHIC 4/7/1980    100251  13      39:00.0 
5           TEST2       DEMOGRAPHIC 8/31/1938   3       12      36:00.0 
21          ZACHARY     EFRON       11/2/1976   100267  24      11:00.0 
16          PATIENT     DEMO        2/28/1943   100260  26      56:00.0 
17          ALICE       WONDERLAND  9/20/1942   100261  20      14:00.0
23          TEST5       BROWN       5/6/1965    15285   27      40:00.0
23          Test5       BROWN       5/6/1965    15285   27      40:00.0

如何使用上面的脚本并将其重构为仅显示Test5 Brown一次?

2 个答案:

答案 0 :(得分:0)

如果您没有任何唯一值,则可能需要ROW_NUMBER来帮助查找每个唯一键的第一条记录:

 SELECT * FROM (
  SELECT 
  v.PatientID,
  p.firstname, 
  p.lastname, 
  p.dob, 
  p.mrn, 
  s.VisitID,
  v.AdmitedDate,
  ROW_NUMBER() OVER (PARTITION BY p.mrn ORDER BY AdmitedDate DESC) AS row_nm
  FROM
    tblPatient p
    JOIN tblPatientVisit v ON p.id = v.PatientID
    JOIN tblPatientSmokingScreenOrder s ON v.id = s.VisitID
    JOIN DESCRIPTOR t ON s.SMOKING_STATUS_DSC_ID = DESCRIPTOR_ID
  WHERE 
    isdate(p.DOB) = 1 
    AND CONVERT(date,p.DOB) <'12/10/2000' 
    AND v.PatientType = 'I' and 
    isdate(v.AdmitedDate) = 1 
    AND CONVERT(date,v.AdmitedDate) > '06/16/2013 16:16'
 ) res
 WHERE row_nm = 1

在这里,您需要通过在ROW_NUMBER()函数中使用不同的ORDER BY列来控制使用mrn显示哪个患者。

如果你有一个唯一的值(mrn是唯一的,例如admiteddate),你可以使用group by来找到每个mrn的最小值/最大值(值),并进行连接以得到其他值,如:

 SELECT * from Table JOIN 
   (Select mrn, MAX(admitedDate) max_date from Table ) u
 ON Table.mrn = u.mrn AND Table.AdmitedDate = u.max_date

答案 1 :(得分:-1)

您可能只需要使用distinct关键字。

SELECT DISTINCT
  v.PatientID,
  p.firstname, 
  p.lastname, 
  p.dob, 
  p.mrn, 
  s.VisitID,
  v.AdmitedDate

FROM
  tblPatient p
  JOIN tblPatientVisit v ON p.id = v.PatientID
  JOIN tblPatientSmokingScreenOrder s ON v.id = s.VisitID
  JOIN DESCRIPTOR t ON s.SMOKING_STATUS_DSC_ID = DESCRIPTOR_ID

WHERE 
  isdate(p.DOB) = 1 
  AND CONVERT(date,p.DOB) <'12/10/2000' 
  AND v.PatientType = 'I' and 
  isdate(v.AdmitedDate) = 1 
  AND CONVERT(date,v.AdmitedDate) > '06/16/2013 16:16'    
--    order by MRN

这将返回所有唯一的行。