如何仅从此查询返回唯一行?

时间:2015-05-08 02:37:28

标签: sql oracle

在SQL中,我有以下SQL(oracle)查询:

SELECT  
     hur.reg_request_id AS "Request No", 
     RESPONSIBILITY_NAME  AS "Module",    
     to_char(hur.creation_date, 'DD-Mon-YYYY HH24:MI:SS') 
          AS "Submitted" ,  /* Dy DD-Mon-YYYY HH24:MI:SS */
     to_char(hur.last_update_date,    'DD-Mon-YYYY HH24:MI:SS') AS "Supervisor"  ,  /* COMMA */          

     ROUND((hur.last_update_date - hur.creation_date), 3 )      
          || ' days' AS "Avg Supervisor Appr Duration" ,   
         hura.creation_date AS "Responsibility" , NULL AS "Avg Resp Appr Duration", NULL AS "Training",
            NULL AS "Avg Training Appr Duration" , 
     hur.LAST_UPDATE_DATE AS "Security", NULL AS "Avg Security Appr Duration",
     NULL AS "SOD", 
     NULL AS "Average SOD Approval Duration" , 
     NULL AS "Configuration",            
     NULL AS "Avg Config Appr Duration",               
     hurr.last_update_date AS "Approved", 
     ROUND( hurr.last_update_date - hur.creation_date, 2 )  
         AS  "Total Overall Duration",
     NULL AS "Notes"
     FROM HHS_UMX_REG_SERVICES hur ,  
     FND_RESPONSIBILITY_VL frt , 
     HHS_UMX_REG_REQUESTS hurr  , 
     HHS_UMX_RESP_ACTIVITY hura

  WHERE hur.RESPONSIBILITY_ID = frt.RESPONSIBILITY_id
             AND 
     hur.RESPONSIBILITY_APPLICATION_ID = frt.APPLICATION_ID           
             AND hurr.reg_request_id  = hur.reg_request_id
             AND hura.created_by = hur.created_by
             AND hura.creation_date >  hur.creation_date /* AND (hur.creation_date + 100 ) */

               AND hur.reg_request_id IN



               /* Manually filling in the 6-digit requisition numbers,based

               on the FDA UFMS User Prov. inbox usually from last 3 days */

                ('261507')

             ORDER BY hur.reg_request_id ASC

问题在于它返回了重复的行。我尝试将DISTINCT添加到查询中,但这并不起作用。

谢谢

1 个答案:

答案 0 :(得分:1)

如果您有重复的行,那么您可能正在加入一个您应该执行相关子查询的子表。

我从查询语义中怀疑问题是HHS_UMX_REG_SERVICES和HHS_UMX_RESP_ACTIVITY之间的连接,它应该是EXISTS相关子查询。您还应该使用ANSI连接语法(并使代码更具可读性)

...
from hhs_umx_reg_services  hur
join fnd_responsibility_vl frt  on (hur.responsibility_id             = frt.responsibility_id and 
                                    hur.responsibility_application_id = frt.application_id ) 
join hhs_umx_reg_requests  hurr on (hurr.reg_request_id               = hur.reg_request_id )
where exists (
        select null
        from   hhs_umx_resp_activity hura
        where  hura.created_by    = hur.created_by and
               hura.creation_date > hur.creation_date)
...

子查询将实现为半连接,这将比内连接更有效。