从视图oracle中删除重复的行

时间:2018-05-09 09:23:12

标签: sql oracle plsql

我有一个带有一些重复数据的oracle视图我正试图从我的视图中删除那些重复的行:

这是我的观看代码:

 CREATE OR REPLACE FORCE VIEW "GLSID"."VW_GLSID_DOCUMENTS" ("ID", "PARENT_ID", "DOCUMENT_TYPE_ID", "AMOUNT") AS 
  SELECT  container.id,
   container.container_id AS parent_id,
   document.document_type_id,
   container.number_of_documents AS amount
   FROM container
   JOIN document ON document.container_id = container.id ;

此视图为我提供了以下结果:

ID | PARENT_ID | DOCUMENT_TYPE_ID | Amount
21      23          3                2
21      23          3                2
21      23          3                1
15      26          3                4

有人可以帮助我从这个视图中删除重复的行来获取这个:

ID | PARENT_ID | DOCUMENT_TYPE_ID | Amount
21      23          3                2
21      23          3                1
15      26          3                4

提前致谢

2 个答案:

答案 0 :(得分:2)

返回结果后,最简单的方法是在SELECT中使用DISTINCT:

 CREATE OR REPLACE FORCE VIEW "GLSID"."VW_GLSID_DOCUMENTS" ("ID", "PARENT_ID", "DOCUMENT_TYPE_ID", "AMOUNT") AS 
  SELECT DISTINCT container.id,
   container.container_id AS parent_id,
   document.document_type_id,
   container.number_of_documents AS amount
   FROM container
   JOIN document ON document.container_id = container.id ;

如果您发现文档计数错误,可以尝试这样做:

 CREATE OR REPLACE FORCE VIEW "GLSID"."VW_GLSID_DOCUMENTS" ("ID", "PARENT_ID", "DOCUMENT_TYPE_ID", "AMOUNT") AS 
  SELECT container.id,
         container.container_id AS parent_id,
         document.document_type_id,
         SUM(container.number_of_documents) AS amount
   FROM container
   JOIN document ON document.container_id = container.id 
   GROUP BY container.id,
            container.container_id,
            document.document_type_id

答案 1 :(得分:1)

也许您应该使用 DISTINCT 运算符

CREATE OR REPLACE FORCE VIEW "GLSID"."VW_GLSID_DOCUMENTS" ("ID", "PARENT_ID", "DOCUMENT_TYPE_ID", "AMOUNT") AS 
SELECT DISTINCT container.id,
container.container_id AS parent_id,
document.document_type_id,
container.number_of_documents AS amount
FROM container
JOIN document ON document.container_id = container.id ;