Oracle 去重查詢

Oracle 去重查詢

CreateTime--2018年2月28日15:38:45

Author:Marydon

(一)使用distincthtml

--查詢指定區間內錶停診字段的值
SELECT DISTINCT T.CLOSE_TZ
  FROM CONSULT_SCHEDULE T
 WHERE T.SCHEDULE_DATE BETWEEN TO_DATE('2018-01-01', 'yyyy-MM-dd') AND
       TO_DATE('2018-02-28', 'yyyy-MM-dd');

說明:sql

  使用distinct關鍵字,後面跟一個字段,則只對該字段的值進行去重;oracle

  後面跟2個字段,則表示column1+column2兩個字段不徹底一致進行去重。spa

(二)對指定字段去重後,再查出該行數據的其餘字段信息3d

UpdateTime--2017年7月10日10:54:20code

1.2.5 對某字段進行去重後,根據這個字段查出在表中所對應的記錄htm

實例1: 查詢指定醫院對應科室下屬的醫生(須要去重)blog

查詢結果字段:醫生id,醫生姓名,醫療機構id,科室idget

  sql1:沒有根據醫生id進行去重,只查出了所需字段class

--查詢排班表中字段:醫生ID,醫生姓名,醫療機構ID,科室ID
SELECT T.DOCTOR_ID    AS FDOCTORCODE,
       T.DOCTOR_NAME  AS FDOCTORNAME,
       T.DOCTOR_PHONE AS FDOCTORPHONE,
       T.ORG_ID       AS FORGID,
       T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T
 WHERE ORG_ID = '416211338'
   AND DEPENT_ID = '1004'

  查詢結果:有不少重複記錄

 

  sql2:根據醫生id進行去重,沒有查出所需字段

--實現效果:根據已知條件對醫生id進行去重查詢
--查詢條件:醫療機構id和科室id
--對查詢結果進行分組
--查詢字段:rowid和計數
--查詢結果:查出每組排班信息中取最大的rowid
SELECT MAX(ROWID),COUNT(1)
  FROM CONSULT_SCHEDULE
 WHERE ORG_ID = '416211338'
   AND DEPENT_ID = '1004'
 GROUP BY DOCTOR_ID 

  查詢結果: 按醫生id進行分組後,取每組記錄中最大的rowid

  sql3:完美實現

--根據條件(指定醫療機構,指定科室)對醫生信息進行去重並查詢出指定字段(醫生id,醫生名稱,醫生電話,機構id,科室id)
SELECT T.DOCTOR_ID    AS FDOCTORCODE,
       T.DOCTOR_NAME  AS FDOCTORNAME,
       T.DOCTOR_PHONE AS FDOCTORPHONE,
       T.ORG_ID       AS FORGID,
       T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T
 WHERE T.ROWID IN (SELECT MAX(ROWID)
                     FROM CONSULT_SCHEDULE
                    WHERE ORG_ID = '416211338'
                      AND DEPENT_ID = '1004'
                    GROUP BY DOCTOR_ID)

  sql4:完美實現

SELECT T.DOCTOR_ID    AS FDOCTORCODE,
       T.DOCTOR_NAME  AS FDOCTORNAME,
       T.DOCTOR_PHONE AS FDOCTORPHONE,
       T.ORG_ID       AS FORGID,
       T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T,
       (SELECT MAX(ROWID) ROWID2
          FROM CONSULT_SCHEDULE
         WHERE ORG_ID = '416211338'
           AND DEPENT_ID = '1004'
         GROUP BY DOCTOR_ID) T2
 WHERE T.ROWID = T2.ROWID2

  sql5:推薦使用

SELECT T.DOCTOR_ID AS FDOCTORCODE,
      max(T.DOCTOR_NAME) AS FDOCTORNAME,
      max(T.DOCTOR_PHONE) AS FDOCTORPHONE,
      max(T.ORG_ID) AS FORGID,
      max(T.DEPENT_ID) AS FDEPTCODE
 FROM CONSULT_SCHEDULE T
WHERE ORG_ID = '134557'
  AND DEPENT_ID = '1004'
GROUP BY DOCTOR_ID

 

  查詢結果:

  錯誤實現方式一:

    使用distinct實現

--錯誤方式一
SELECT DISTINCT T.DOCTOR_ID    AS FDOCTORCODE,
                T.DOCTOR_NAME  AS FDOCTORNAME,
                T.DOCTOR_PHONE AS FDOCTORPHONE,
                T.ORG_ID       AS FORGID,
                T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T
 WHERE T.ORG_ID = '416211338'
   AND T.DEPENT_ID = '1004' 

  錯誤結果: 

    錯在哪:

       distinct的用法是:

      a.distinct + 單個字段,表示對該字段進行去重處理;

      b.distinct + column1,column2,。。。,表示的是使用n個字段進行聯合去重,即查出來的是這n個字段的值相加結果不一致的數據;

         而不是:對第一個字段作去重處理後,再將其餘字段查詢出來。

    錯誤實現方式二 

--錯誤方式二
SELECT T.DOCTOR_ID    AS FDOCTORCODE,
       T.DOCTOR_NAME  AS FDOCTORNAME,
       T.DOCTOR_PHONE AS FDOCTORPHONE,
       T.ORG_ID       AS FORGID,
       T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T
 WHERE T.DOCTOR_ID IN (SELECT DISTINCT T2.DOCTOR_ID
                         FROM CONSULT_SCHEDULE T2
                        WHERE T2.ORG_ID = '416211338'
                          AND T2.DEPENT_ID = '1004')

    錯誤結果:

    錯在哪:

      先用distinct雖然查出來的醫生id具備惟一性,可是, 根據這個惟一的醫生id結果集去查詢其餘字段數據的結果沒法保證數據的惟一性

    實例2:查CONSULT_SCHEDULE表中字段:醫生id,姓名,醫療機構id,科室id並根據醫生id去重

      sql實現:

SELECT T.DOCTOR_ID    AS FDOCTORCODE,
       T.DOCTOR_NAME  AS FDOCTORNAME,
       T.DOCTOR_PHONE AS FDOCTORPHONE,
       T.ORG_ID       AS FORGID,
       T.DEPENT_ID    AS FDEPTCODE
  FROM CONSULT_SCHEDULE T,
       (SELECT MAX(ROWID) ROWID2 FROM CONSULT_SCHEDULE GROUP BY DOCTOR_ID) T2
 WHERE T.ROWID = T2.ROWID2 

      查詢結果:

 

相關文章
相關標籤/搜索