我正在编写一个SQL查询,我的目标是根据列名找到重复的值:
SELECT a.PROJECTNAME
, a.OBJECTTYPE
, a.OBJECTID1
, a.OBJECTVALUE1
, a.OBJECTID2
, a.OBJECTVALUE2
, a.OBJECTID3
, a.OBJECTVALUE3
, a.OBJECTID4
, a.OBJECTVALUE4
FROM PSPROJECTITEM a
WHERE a.projectname = 'AZ_HCM_745'
AND 1 < (
SELECT COUNT(*)
FROM PSPROJECTITEM c
WHERE 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)
ORDER BY a.projectname
我的目的是找到那些与a.projectname
重复的值,我的意思是,输出应显示AZ_HCM_745
的重复值,意味着它应该具有相同的字段,如objecttype
},objectid
甚至是对象的计数。
我正在寻找这样的输出:
PROJECTNAME OBJECTTYPE OBJECTID1 OBJECTVALUE1 OBJECTID2 OBJECTVALUE2 OBJECTID3 OBJECTVALUE3 OBJECTID4 OBJECTVALUE4
这些是我从查询中选择的字段名称。
现在我传递a.projectname ='AZ_HCM_745'。
我的目标是找到与AZ_HCM_745重复的数据和具有这些值的项目名称,例如:
AZ_HCM_745 0 1 AUDIT_AZ_ADP11P 0 0 0
是原始值。
重复的值是:
AZ_HCM_745_BKP 0 1 AUDIT_AZ_ADP11P 0 0 0
请注意projectname可以变化,_bkp或_a,我的目标是找到具有objecttype,objectid1重复值的项目名,我想选择这些值。
此外,查询必须仅获取在参数中传递的项目名称的重复值,而不是项目名称,表示不得显示原始值,只显示重复项
正在使用的数据库是Oracle。
答案 0 :(得分:4)
您的查询的主要问题是它是“由内而外”。也就是说,您只在外部查询中选择感兴趣的项目。内部正在选择所有类似的内部,但它们没有输出,因为它们在where
子句中。
尝试此变体:
SELECT a.PROJECTNAME
, a.OBJECTTYPE
, a.OBJECTID1
, a.OBJECTVALUE1
, a.OBJECTID2
, a.OBJECTVALUE2
, a.OBJECTID3
, a.OBJECTVALUE3
, a.OBJECTID4
, a.OBJECTVALUE4
FROM PSPROJECTITEM a
WHERE a.projectname <> 'AZ_HCM_745' and
exists (
SELECT *
FROM PSPROJECTITEM c
WHERE c.projectname = 'AZ_HCM_745' 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)
ORDER BY a.projectname
次要考虑因素是NULL
值不匹配(如果这是一个问题,您需要使用coalesce()
之类的内容。)
答案 1 :(得分:0)
如果我错了,请纠正我,你的目标是使用项目名称在PSPROJECTITEM中找到重复的值。
SELECT t2.objecttype
,t2.objectid1
,t2.objectvalue1
,t2.objectid2
,t2.objectvalue2
,t2.objectid3
,t2.objectvalue3
,t2.objectid4
,t2.objectvalue4
FROM PSPROJECTITEM t2, PROJECTNAME t1
WHERE t2.objecttype = t1.objecttype
AND t2.objectid1 = t1.objectid1
AND t2.objectvalue1 = t1.objectvalue1
AND t2.objectid2 = t1.objectid2
AND t2.objectvalue2 = t1.objectvalue2
AND t2.objectid3 =t1.objectid3
AND t2.objectvalue3 = t1.objectvalue3
AND t2.objectid4 = t1.objectid4
AND t2.objectvalue4 = t1.objectvalue4
AND t1.projectname = 'AZ_HCM_745'
如果您的数据库中有唯一ID,请使用它
答案 2 :(得分:-1)
SELECT a.PROJECTNAME,
count(a.PROJECTNAME) as count
FROM PSPROJECTITEM a
GROUP BY a.PROJECTNAME HAVING count(a.PROJECTNAME) >1
答案 3 :(得分:-1)
你可以试试这个:
SELECT A.PROJECTNAME,A.objectid, A.objecttype ,COUNT(*) DUBLICATE FROM PSPROJECTITEM A GROUP BY A.PROJECTNAME,A.objectid, A.objecttype HAVING COUNT(*)>1