获得额外的重复值

时间:2013-06-19 05:46:07

标签: sql oracle

我的要求是:

我必须从表PSPROJECTITEM中找到在查询中传递的PROJECTNAME的重复项。目标是找出在创建备份或迁移期间出现的重复项目。行的重复必须在两个项目之间进行检查,如果它们有类似的对象,如objecttype,objectid1等,并且这些对象的计数匹配,则它们是重复的。

我的问题是我写的查询会获得额外的副本。

SELECT a.PROJECTNAME 
 , a.OBJECTTYPE 
 , a.OBJECTID1 
 , a.OBJECTVALUE1 
 , a.OBJECTID2 
 , a.OBJECTVALUE2 
 , a.OBJECTID3 
 , a.OBJECTVALUE3 
 , a.OBJECTID4 
 , a.OBJECTVALUE4,
 COUNT(*) as duplicate
  FROM PSPROJECTITEM a 
 WHERE a.projectname <> 'AZ_11' and
       exists ( 
 SELECT *
  FROM PSPROJECTITEM c 
 WHERE c.projectname  = 'AZ_11' 
   and a.objecttype = c.objecttype 
   AND a.objectid1 =c.objectid1 
   AND a.objectvalue1 = c.objectvalue1 
   AND a.objectid2 = c.objectid2 
   AND a.objectvalue2 = c.objectvalue2 
   AND a.objectid3 = c.objectid3 
   AND a.objectvalue3 = c.objectvalue3 
   AND a.objectid4 = c.objectid4 
   AND a.objectvalue4 = c.objectvalue4) 
   group by a.PROJECTNAME 
 , a.OBJECTTYPE 
 , a.OBJECTID1 
 , a.OBJECTVALUE1 
 , a.OBJECTID2 
 , a.OBJECTVALUE2 
 , a.OBJECTID3 
 , a.OBJECTVALUE3 
 , a.OBJECTID4 
 , a.OBJECTVALUE4
  ORDER BY a.projectname

必须在对象的基础上进行匹配,并且复制是具有相同对象的对象,并且对象的计数也匹配。

记录PSPROJECTITEM中projectname的副本是

A_RP    7   10  AZ_EP_DASHBOARD 39  GBL 0       0    
A_RP    46  10  AZ_EP_DASHBOARD 39  GBL 12  PostBuild   0    
A_RP    46  10  AZ_EP_DASHBOARD 39  GBL 12  PreBuild    0    
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ACCEPTED       FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ACCPTRQST_LINK FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ADD_PEER       FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ALL_REQUESTS   FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_COMPLETED      FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DASH_RTNXFR_OK FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DECLINED       FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW     FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW2    FieldChange
A_RP    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DEL_NO         FieldChange

查询还会获取这些值:

AZ_ASH_DASHBOARD    7   10  AZ_EP_DASHBOARD 39  GBL 0       0    
AZ_ASH_DASHBOARD    46  10  AZ_EP_DASHBOARD 39  GBL 12  PostBuild   0    
AZ_ASH_DASHBOARD    46  10  AZ_EP_DASHBOARD 39  GBL 12  PreBuild    0    
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ACCEPTED       FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ADD_PEER       FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_COMPLETED      FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DECLINED       FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW     FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW2    FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DEL_NO         FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DEL_YES        FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLDOWN_BTN  FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLTOP_BTN   FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLUP_BTN    FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EERVW_ACTION   FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_ACCEPT      FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_DECLINE     FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_FDBCK_LINK  FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_OLD_RVW_LINKFieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GOALS_ACTION   FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_MANAGE_FDBCK   FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_MGRRVW_ACTION  FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_NOMINATED      FieldChange
AZ_ASH_DASHBOARD    48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_NOTIFY_PB      FieldChange

意思是,这些是附加值,除此之外,额外的值是这样的。

请帮助我完成此查询,以便获取正在查询中传递的项目的副本。

正在使用的数据库是oracle。

这是

的结果
select * from PSPROJECTITEM where PROJECTNAME = 'AZ_11';

列名:

PROJECTNAME OBJECTID1 OBJECTVALUE1 OBJECTID2 OBJECTVALUE2 OBJECTID3 OBJECTVALUE3 OBJECTID4 OBJECTVALUE4

AZ_11   7   10  AZ_EP_DASHBOARD 39  GBL 0       0    
AZ_11   46  10  AZ_EP_DASHBOARD 39  GBL 12  PostBuild   0    
AZ_11   46  10  AZ_EP_DASHBOARD 39  GBL 12  PreBuild    0    
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ACCEPTED       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ACCPTRQST_LINK FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ADD_PEER       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_ALL_REQUESTS   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_COMPLETED      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DASH_RTNXFR_OK FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DECLINED       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW     FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DELETE_ROW2    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DEL_NO         FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DEL_YES        FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLDOWN_BTN  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLTOP_BTN   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_DRILLUP_BTN    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EERVW_ACTION   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_ACCEPT      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_DASH_CANCEL FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_DECLINE     FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_FDBCK_LINK  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_EP_OLD_RVW_LINKFieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GIVEACCPT_FDBCKFieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GIVE_FDBCK     FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GL_CREATE_LINK FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GL_CREATE_MGR  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GL_LIST_LINK   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GL_LIST_MGR    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_GOALS_ACTION   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_MANAGE_FDBCK   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_MGRRVW_ACTION  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_NOMINATED      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_NOTIFY_PB      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_PCS_LINK       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_PENDING        FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_REQUEST_FDBCK  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_REQUEST_FDBCKS FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_REVIEW_ACTION  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_RQST_FDBCK     FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_TOTAL_REQUESTS FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_TRANSFER_LINK  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_EP_DASH_WRK  2   AZ_UNSOLICITED    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_ARCHIVE_FLG    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_CANCEL         FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_FBFILTER       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_FB_COMPL_LINK  FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_FB_RQST_LINK   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_REWORK_PB      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   AZ_SUBMIT_LINK    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   RETURN_LINK       FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   SELECT_ALL        FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_FDBCK_WRK    2   TOGGLE1           FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_CANCEL         FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_COPY           FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_COPY_LINK      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_EDIT_LINK      FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GL_CREATE_LINK FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GOAL_FILTER    FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GOAL_FILTER1   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GOAL_FILTER2   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GOAL_FILTER3   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_GOAL_FILTER4   FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   AZ_GOALS_WRK    2   AZ_SELECT_ALL     FieldChange
AZ_11   48  10  AZ_EP_DASHBOARD 39  GBL 1   DERIVED_PSS 2   SEARCH_BTN        FieldChange

如果我提前得到了帮助,请告诉我是否遗漏了任何信息。

这是用于测试重复项的查询:

(SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='A_RP') 
union all
( 
 SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='A_RP'
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
);

为此,它返回空行。

当我使用这些值运行时:

(SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_ASH_DASHBOARD ') 
union all
( 
 SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_ASH_DASHBOARD '
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
);

这种影响:

65
0

这意味着,这些不是重复的项目,因为对象不匹配。

只是帮助我解决这个问题,这非常复杂,而且我的想法不合理。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,项目计为重复,那么它与另一个项目具有相同的行数。尝试在子查询中添加此条件,例如:

SELECT a.PROJECTNAME 
 , a.OBJECTTYPE 
 , a.OBJECTID1 
 , a.OBJECTVALUE1 
 , a.OBJECTID2 
 , a.OBJECTVALUE2 
 , a.OBJECTID3 
 , a.OBJECTVALUE3 
 , a.OBJECTID4 
 , a.OBJECTVALUE4,
 COUNT(*) as duplicate
  FROM PSPROJECTITEM a 
 WHERE a.projectname <> 'AZ_11' and
       exists ( 
 SELECT *
  FROM PSPROJECTITEM c 
 WHERE c.projectname  = 'AZ_11' 
   and a.objecttype = c.objecttype 
   AND a.objectid1 =c.objectid1 
   AND a.objectvalue1 = c.objectvalue1 
   AND a.objectid2 = c.objectid2 
   AND a.objectvalue2 = c.objectvalue2 
   AND a.objectid3 = c.objectid3 
   AND a.objectvalue3 = c.objectvalue3 
   AND a.objectid4 = c.objectid4 
   AND a.objectvalue4 = c.objectvalue4) 
   AND (SELECT COUNT(*) FROM PSPROJECTITEM d
   WHERE d.projectname = a.projectname) = 
   (SELECT COUNT(*) FROM PSPROJECTITEM e
   WHERE e.projectname = 'AZ_11')
   group by a.PROJECTNAME 
 , a.OBJECTTYPE 
 , a.OBJECTID1 
 , a.OBJECTVALUE1 
 , a.OBJECTID2 
 , a.OBJECTVALUE2 
 , a.OBJECTID3 
 , a.OBJECTVALUE3 
 , a.OBJECTID4 
 , a.OBJECTVALUE4
  ORDER BY a.projectname

另外,我认为,此查询会返回此结果,因为项目名称末尾有空格“AZ_ASH_DASHBOARD”:

(SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_ASH_DASHBOARD ') 
union all
( 
 SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_ASH_DASHBOARD '
minus
SELECT COUNT(*) FROM PSPROJECTITEM
WHERE PROJECTNAME='AZ_11'
);