SQL query debugging

Status
Not open for further replies.

Trilby

Ars Tribunus Militum
1,690
Hi there,

I need to debug this query which is causing us problems and it's been awhile since I've written any SQL. I'll continue to hammer away at this but I'm missing some fundamental concept here and would appreciate a little help.

Code:
    select  distinct 
            contact_dtl.CUSTOMER_ID,
            contact_dtl.SALUTATION_CD,
            contact_dtl.FIRST_NM,
            contact_dtl.LAST_NM,
            contact_dtl.NM_SUFFIX_CD,
            elec_dtl.ELEC_ADDR_TXT,
            sysdate as CREATE_TS
    from
        (
            select  /*+ index(cust_contact ODSCM.CUST_CONTACT_INDVDL_ASGNMT_IX1) */
                    distinct
                    cust_contact.CUSTOMER_ID,
                    cust_contact.CONTACT_INDVDL_ID,
                    INITCAP(contact.SALUTATION_CD) as SALUTATION_CD,
                    INITCAP(contact.FIRST_NM) as FIRST_NM,
                    INITCAP(contact.LAST_NM) as LAST_NM,
                    INITCAP(contact.NM_SUFFIX_CD) as NM_SUFFIX_CD
            from    
                    TABLE_A cust_contact, 
                    TABLE_B contact
            where   
                    cust_contact.CUSTOMER_ID IN
                        (
                            select  distinct
                                    customer_id
                            from
                                    TABLE_C
                            
                        )
                    and cust_contact.CONTACT_ROLE_TYPE_CD = 'N'
                    and (cust_contact.EFF_END_DT is null or cust_contact.EFF_END_DT > sysdate)
                    and cust_contact.CUSTOMER_ID = contact.CUSTOMER_ID
                    and (contact.FIRST_NM is not null or contact.FIRST_NM != '')
                    and (contact.LAST_NM is not null or contact.LAST_NM != '')
        ) 
            contact_dtl,

        (
            select  elec.CONTACT_INDVDL_ID,
                    elec.ELEC_ADDR_TXT
            from 
                    TABLE_D elec
            where   
                    elec.ELEC_ADDR_TYPE_CD = 'E'
                and elec.ELEC_ADDR_SUBTYPE_CD = 'EPR'
                and (elec.EFF_END_DT is null or elec.EFF_END_DT > sysdate)
        ) 
            elec_dtl
                
    where contact_dtl.CONTACT_INDVDL_ID = elec_dtl.CONTACT_INDVDL_ID (+);

Now this issue is that this returns 'duplicate' rows like this:

Code:
CUSTOMER_ID    SALUT    FIRST   LAST            EMAIL           CREATE_TS
12345678		John    Smith		jsmith@abc.com	30/05/2012 14:26
12345678	Mr	Smith	Smith		jsmith@abc.com	30/05/2012 14:26

*I know the column headers aren't exact, I had to truncate some as the formatting wasn't behaving.
I think what I need to do is use a CASE statement to identify when a SALUTATION_CD exists and then only select the row once but I'm still struggling with how to implement it.
 

Trilby

Ars Tribunus Militum
1,690
svdsinner":3nyn9d5c said:
First thing I would do is to re-write it with modern join syntax. That will make the cause of the duplication much easier to spot.

What RDMS are you using?

Good thinking. I never wrote the thing so I'm hesitant to change too much, much easer to do it that way. It's Oracle (10g, I think).

Boom done, stupid sub-queries:

Code:
select  
    distinct
    cust_contact.CUSTOMER_ID,
    INITCAP(contact.SALUTATION_CD) as SALUTATION_CD,
    INITCAP(contact.FIRST_NM) as FIRST_NM,
    INITCAP(contact.LAST_NM) as LAST_NM,
    INITCAP(contact.NM_SUFFIX_CD) as NM_SUFFIX_CD,
    elec.ELEC_ADDR_TXT,
    sysdate as CREATE_TS    
from    
      TABLE_A cust_contact
JOIN     
     TABLE_B contact
ON 
     cust_contact.CUSTOMER_ID = contact.CUSTOMER_ID
JOIN 
     TABLE_C elec
ON
     cust_contact.CONTACT_INDVDL_ID = elec.CONTACT_INDVDL_ID
where   
      cust_contact.CUSTOMER_ID IN
      (
          select  distinct
              customer_id
          from
              VIEW_A

      )
and cust_contact.CONTACT_ROLE_TYPE_CD = 'N'
and (cust_contact.EFF_END_DT is null or cust_contact.EFF_END_DT > sysdate)
and (contact.FIRST_NM is not null or contact.FIRST_NM != '')
and (contact.LAST_NM is not null or contact.LAST_NM != '')
and elec.ELEC_ADDR_TYPE_CD = 'E'
and elec.ELEC_ADDR_SUBTYPE_CD = 'EPR'
and (elec.EFF_END_DT is null or elec.EFF_END_DT > sysdate)

I'm still getting the replicated rows with and without the salutation_cd because TABLE_B contact has multiple rows for each customer with and without the salutation. How do I get it to only pull the one with a salutation, if it exists.

EDIT:

So my first thought was replacing the
Code:
JOIN     
     TABLE_B contact

with

Code:
JOIN     
     SELECT * FROM 
     TABLE_B 
     GROUP BY CUSTOMER_ID
     HAVING (COUNT(CUSTOMER_ID) = 1))
UNION
... same thing but with a CASE statement for COUNT >1

EDIT: Man I wished we documented things better. I solved it by poking around the DB and it turns out there was a CURRENT_RECORD_IND column that I just needed to fill for.
 
Status
Not open for further replies.