一个字段相对于另一个字段的重复值

时间:2019-05-27 13:47:38

标签: sql oracle

我想获得一个字段相对于其他字段的重复值。 RAP07C.SSITYP上RAP07C.SSISEQ的重复值

对于RAP07C.SSITYP,我使用了“ HAVING COUNT(RAP07C.SSISEQ)> 1”,但是,即使RAP07C.SSISEQ的两个值相对于RAP07C.SSISEQ而言,它们具有两个或多个记录,我仍在获取记录。 RAP07C.SSITYP。但是我想要具有不同RAP07C.SSISEQ值的记录。

SELECT DISTINCT 
  /*+ use_hash(RAP01 rap07a RAP07C) */
  Rap01.plcy,
  Rap01.j01_PT_LINE_cat_Cd AS j01_PT_LINE_cat_Cd,
  Rap01.J01_Pt_State_Cd    AS J01_Pt_State_Cd,
  COUNT(RAP07C.SSISEQ)     AS Count_of_SSISEQ,
  --RAP07C.SSISEQ,
  RAP07C.SSITYP
  --
FROM Rap01
  --
JOIN RAP07C
ON RAP07C.J41_PT_LINE_CAT_CD  = rap01.j01_pt_line_cat_cd
AND RAP07C.J41_PT_CDB_PART_ID = rap01.j01_pt_cdb_part_id
AND RAP07C.J41_PT_STATE_CD    = rap01.j01_pt_state_cd
AND RAP07C.PLCY               = rap01.plcy
AND RAP07C.dropdt_t           = '31-DEC-9999'
  --
WHERE Rap01.Line3 IN ('010','016')
GROUP BY RAP01.plcy,
  j01_PT_LINE_cat_Cd,
  J01_Pt_State_Cd,
  RAP07C.SSITYP
HAVING COUNT(RAP07C.SSISEQ) > 1;

相对于RAP07C.SSITYP,我想要不同的RAP07C.SSISEQ值 就像:IFS-001 IFS-001,因为相对于IFS,我获得了两个以上的值001,但是我需要为IFS(RAP07C.SSITYP)获得的值仅是IFS-001 IFS-002。

1 个答案:

答案 0 :(得分:1)

尝试将DISTINCT一词放入COUNT。

SELECT 
  Rap01.plcy,
  Rap01.j01_PT_LINE_cat_Cd AS j01_PT_LINE_cat_Cd,
  Rap01.J01_Pt_State_Cd    AS J01_Pt_State_Cd,
  COUNT(DISTINCT RAP07C.SSISEQ)     AS Count_of_SSISEQ,
  RAP07C.SSITYP

FROM Rap01

JOIN RAP07C
ON RAP07C.J41_PT_LINE_CAT_CD  = rap01.j01_pt_line_cat_cd
AND RAP07C.J41_PT_CDB_PART_ID = rap01.j01_pt_cdb_part_id
AND RAP07C.J41_PT_STATE_CD    = rap01.j01_pt_state_cd
AND RAP07C.PLCY               = rap01.plcy
AND RAP07C.dropdt_t           = '31-DEC-9999'

WHERE Rap01.Line3 IN ('010','016')
GROUP BY RAP01.plcy,
  j01_PT_LINE_cat_Cd,
  J01_Pt_State_Cd,
  RAP07C.SSITYP
HAVING COUNT(DISTINCT RAP07C.SSISEQ) > 1;