使用group by方法时如何删除重复的行?

时间:2019-02-27 11:17:40

标签: sql-server

我通过该方法使用分组来区分两个列的值。但是我仍然在列行中得到了重复的值。

    SELECT MRD_NO,RESOURCE_NAME,
Diagnosis = 
    STUFF((SELECT DISTINCT ', ' + 
     (case when Diagnosis is null and OTHER_DIAGONSIS is not null then OTHER_DIAGONSIS else Diagnosis end)
           FROM EMR_master b 
           WHERE b.MRD_NO = a.MRD_NO 
          FOR XML PATH('')), 1, 2, '') 
FROM EMR_master a 
where
a.TREATMENT_CODE in ('CC','PO','SRE','REG') 
group by MRD_NO,RESOURCE_NAME
order by RESOURCE_NAME,MRD_NO

我的输出在MRD_NO列中包含重复的值,请告诉我如何删除重复的值。

我的输出

MRD_NO |  RESOURCE_NAME  | Diagnosis
123    |  james          | retina 
126    |  peter          | throat pain 
129    |  Murugan        | fever
129    |  william        | fever
130    |  william        | retina

我需要这样的输出

MRD_NO |  RESOURCE_NAME  | Diagnosis
    123    |  james          | retina 
    126    |  peter          | throat pain 
    129    |  Murugan        | fever        
    130    |  william        | retina

注意:,我得到了带有两个资源名称(Murugan,william)的重复MRD_NO 129, 所以我需要消除意志并获得独特的MRD_NO

2 个答案:

答案 0 :(得分:0)

尝试在DISTINCT运算符中选择一列:

SELECT MRD_NO,RESOURCE_NAME,
Diagnosis = 
    STUFF((SELECT DISTINCT b.MRD_NO + ', ' + 
     (case 
          when Diagnosis is null and OTHER_DIAGONSIS is not null 
              then OTHER_DIAGONSIS else Diagnosis end)
      FROM EMR_master b 
      WHERE b.MRD_NO = a.MRD_NO 
      GROUP BY b.MRD_NO, b.RESOURCE_NAME
      FOR XML PATH('')), 1, 2, '') 
FROM EMR_master a 
where
a.TREATMENT_CODE in ('CC','PO','SRE','REG') 
group by MRD_NO,RESOURCE_NAME
order by RESOURCE_NAME,MRD_NO

例如:

DECLARE @table TABLE 
(
EmpID int, 
EmpName varchar(50),    
DateOfJoin datetime,
DateOfLeaving DATETIME,
StatusName VARCHAR(50)
)

INSERT INTO @table
(
    EmpID,
    EmpName,
    DateOfJoin,
    DateOfLeaving,
    StatusName
)
VALUES
 (1, 'XYZ',      '2015-10-01',    '2017-09-26', 'De-ACTIVE')
,(2, 'ABC',      '2018-01-01',    NULL, 'ACTIVE')                      
,(3, 'XYZ',      '2018-10-15',    NULL, 'ACTIVE')    

并查询:

SELECT 
hey = STUFF((
        SELECT 
        DISTINCT t.EmpName + ', '
        FROM @table t
        FOR XML PATH('')), 1, 2, '')        
FROM @table t

输出:

hey
C, XYZ, 
C, XYZ, 
C, XYZ, 

更新:

如果您只想删除重复项,则应仅使用GROUP BY来指定要删除重复项的位置。

SELECT
  MRD_NO
, RESOURCE_NAME 
, Diagnosis
FROM YourTable
GROUP BY MRD_NO
, RESOURCE_NAME 
, Diagnosis

答案 1 :(得分:0)

如果您不想花太多时间来获得所需的输出,可以使用临时表

       SELECT MRD_NO,RESOURCE_NAME,     
            STUFF((SELECT DISTINCT ', ' + 
             (case when Diagnosis is null and OTHER_DIAGONSIS is not null then OTHER_DIAGONSIS else Diagnosis end) as 'Diagnosis'

into #tmpEmrDetail
               FROM EMR_master b 
               WHERE b.MRD_NO = a.MRD_NO 
              FOR XML PATH('')), 1, 2, '') 
    FROM EMR_master a 
    where
    a.TREATMENT_CODE in ('CC','PO','SRE','REG') 
    group by MRD_NO,RESOURCE_NAME
    order by RESOURCE_NAME,MRD_NO

select distinct * from #tmpEmrDetail

drop table #tmpEmrDetail

; WITH ctetbl AS
     (
     SELECT MRD_NO,RESOURCE_NAME,
Diagnosis = 
    STUFF((SELECT DISTINCT ', ' + 
     (case when Diagnosis is null and OTHER_DIAGONSIS is not null then OTHER_DIAGONSIS else Diagnosis end)
           FROM EMR_master b 
           WHERE b.MRD_NO = a.MRD_NO 
          FOR XML PATH('')), 1, 2, '') 
FROM EMR_master a 
where
a.TREATMENT_CODE in ('CC','PO','SRE','REG') 
group by MRD_NO,RESOURCE_NAME


     )
     SELECT *
     FROM ctetbl order by RESOURCE_NAME,MRD_NO