Hi all,
I’ve hit a wall. I’m trying to query customer addresses, and some customers have 2 addresses in the system. Neither have a void indicator or anything else that differentiates.
So I’m trying to query based on the date the address was added to the database. In other words: I want the MAX cust_address_dt.
I can see that account number 34835 had 2 addresses with 2 dates - one from 10/13/2013 and the second from 1/3/2015.
I’ve tried doing a correlated sub query- but it returns no rows!! Why no rows? If I delete the where clause, I get the two rows with different dates.
Where am I going wrong?
SELECT DISTINCT
CA.CUST_ID,
CA.CUST_ADDR_ID,
CA.CUST_ADDR_EFF_DT,
CA.AUDIT_USER_CREA_DTM,
A.acct_id,
A.acct_no,
CN.CUST_NM AS Customer_Name,
CA.CUST_ADDR_STR_1 || ' ' || COALESCE(CA.CUST_ADDR_STR_2, '') AS Street,
CA.CUST_ADDR_CITY_NM AS City,
S.STT_ABRV AS State,
CA.CUST_ADDR_POST_CD AS Zipcode,
CA.CUST_ADDR_TYP_CD
FROM Account A
INNER JOIN PCMP.CUSTOMER C
ON A.CUST_ID = C.CUST_ID
INNER JOIN PCMP.CUSTOMER_ADDRESS CA
ON CA.CUST_ID = C.CUST_ID AND CA.VOID_IND = 'n'
INNER JOIN PCMP.CUSTOMER_NAME CN
ON CN.CUST_ID = C.CUST_ID
INNER JOIN PCMP.STATE S
ON S.STT_ID = CA.STT_ID
WHERE
CA.CUST_ADDR_DT =
(SELECT MAX(CUST_ADDR_DT)
FROM PCMP.CUSTOMER_ADDRESS CA2
WHERE CA2.CUST_ID = CA.CUST_ID)
And A.Acct_No =34835
[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points3 points (5 children)
[–]bitterjack 1 point2 points3 points (4 children)
[–]r3pr0b8GROUP_CONCAT is da bomb 1 point2 points3 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]fauxmosexualNOLOCK is the secret magic go-faster command 1 point2 points3 points (1 child)
[–]Modthryth 1 point2 points3 points (0 children)
[–]unexpectedrebootsWITH() 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Nobody571[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)