DISTINCT返回Oracle中的重复行

时间:2018-10-12 18:32:50

标签: sql oracle group-by distinct

我不是SQL专业人士。我在SELECT语句中使用了所有列,并在GROUP BY中也使用了所有列。但是,我得到了重复的记录。然后,我在查询中添加了DISTINCT,但仍然得到重复的记录。我在这里做什么错了?

SELECT DISTINCT 
        action_by_id
       ,a.status_cd
       ,owner_dsply_nm                      AS "CASE OWNER"
       ,case_intrl_id                       AS "CASE ID"
       ,status_nm                           AS "STATUS VALUE"
       ,action_ts                           AS "STATUS CHANGE DATE"
       ,To_char(action_ts - 7 / 24, 'IYYY') AS "STATUS CHANGE YEAR" 
       ,To_char(action_ts - 7 / 24, 'IW')   AS "STATUS CHANGE WEEK" 
FROM   kdd_case_actions a 
       join kdd_status  s 
         ON a.status_cd = s.status_cd 
       join kdd_review_owner r
         ON r.owner_seq_id = a.action_by_id
WHERE  a.status_cd NOT IN ( 'SBTMNEW', 'NW', 'SB01NEW' ) 
GROUP  BY To_char(action_ts - 7 / 24, 'IYYY') 
          ,To_char(action_ts - 7 / 24, 'IW') 
          ,action_ts
          ,case_intrl_id 
          ,status_nm
          ,owner_dsply_nm
          ,action_by_id
          ,a.status_cd

Results

2 个答案:

答案 0 :(得分:2)

如果action_ts列是时间戳或日期,则只能在用户界面中看到它的一部分。 如果您将其打印得更详细,我想您会发现该列上的行具有不同的值。

答案 1 :(得分:1)

查询中没有聚合功能,因此为什么要使用分组依据..仅使用不重复
并确保只获得action_ts的日期部分,最后确保您有适当的修剪字符串以保留空格或不可见的字符

    SELECT DISTINCT 
            action_by_id
           ,trim(a.status_cd)
           ,trim(owner_dsply_nm)                      AS "CASE OWNER"
           ,case_intrl_id                       AS "CASE ID"
           ,trim(status_nm )                         AS "STATUS VALUE"
           ,to_char(action_ts, 'DD.MON.YYYY')    AS "STATUS CHANGE DATE"
           ,To_char(action_ts - 7 / 24, 'IYYY') AS "STATUS CHANGE YEAR" 
           ,To_char(action_ts - 7 / 24, 'IW')   AS "STATUS CHANGE WEEK" 
    FROM   kdd_case_actions a 
    join kdd_status  s ON a.status_cd = s.status_cd 
    join kdd_review_owner r ON r.owner_seq_id = a.action_by_id
    WHERE  a.status_cd NOT IN ( 'SBTMNEW', 'NW', 'SB01NEW' ) 


    TO_DATE (action_ts, 'DD.MON.YYYY')