Hi there. I have the following query, which works fine:
SELECT TMP_CLPUD.dbo.FormatS(RIGHT(SV1.VASLOC, 7), 9, 0) AS C_ACCOUNT,
TMP_CLPUD.dbo.FormatS(Cust.C_CUSTOMER, 7, 0) AS C_CUSTOMER,
CA.OID,
CN2.NBCUNQ AS CUNQ,
CN2.NBSUNQ AS SUNQ,
'AC' AS C_STATUS,
TMP_CLPUD.dbo.mmsDATE(CN2.NBCNTE) AS IN_DATE,
TMP_CLPUD.dbo.mmsDATE(CN2.NBDSTE) AS OUT_DATE,
0 AS ARCHIVE
FROM BILLING.SVP00100 AS SV1 INNER JOIN BILLING.CNP00200 AS CN2 ON SV1.VASUNQ = CN2.NBSUNQ
INNER JOIN BILLING.CMP00100 AS CM1 ON CN2.NBCUNQ = CM1.KACUST
INNER JOIN BILLING.CustomerAccounts AS CA ON CM1.KACUST = CA.CustomerNbr
INNER JOIN TMP_CLPUD.dbo.tblCustomers As Cust ON CA.OID = Cust.OID
INNER JOIN BILLING.Customers AS C ON CA.OID = C.OID
INNER JOIN BILLING.CNP00100 AS CN1 ON CN2.NBCUNQ = CN1.NACUNQ AND CN2.NBSUNQ = CN1.NASUNQ
ORDER BY C_ACCOUNT, C_CUSTOMER
However, if I try to link one more field between CN1 and CN2, the query will hang indefinitely. The field is type decimal(8,0) in both tables and does not allow for nulls. Here is the query with the third link in place:
SELECT TMP_CLPUD.dbo.FormatS(RIGHT(SV1.VASLOC, 7), 9, 0) AS C_ACCOUNT,
TMP_CLPUD.dbo.FormatS(Cust.C_CUSTOMER, 7, 0) AS C_CUSTOMER,
CA.OID,
CN2.NBCUNQ AS CUNQ,
CN2.NBSUNQ AS SUNQ,
'AC' AS C_STATUS,
TMP_CLPUD.dbo.mmsDATE(CN2.NBCNTE) AS IN_DATE,
TMP_CLPUD.dbo.mmsDATE(CN2.NBDSTE) AS OUT_DATE,
0 AS ARCHIVE
FROM BILLING.SVP00100 AS SV1 INNER JOIN BILLING.CNP00200 AS CN2 ON SV1.VASUNQ = CN2.NBSUNQ
INNER JOIN BILLING.CMP00100 AS CM1 ON CN2.NBCUNQ = CM1.KACUST
INNER JOIN BILLING.CustomerAccounts AS CA ON CM1.KACUST = CA.CustomerNbr
INNER JOIN TMP_CLPUD.dbo.tblCustomers As Cust ON CA.OID = Cust.OID
INNER JOIN BILLING.Customers AS C ON CA.OID = C.OID
INNER JOIN BILLING.CNP00100 AS CN1 ON CN2.NBCUNQ = CN1.NACUNQ AND CN2.NBSUNQ = CN1.NASUNQ AND CN2.NBCNTE = CN1.NACNTE
ORDER BY C_ACCOUNT, C_CUSTOMER
I've rewritten this thing as many different ways as I could think but every time I try to link that third field it just hangs forever. Any suggestions?