SELECT * FROM(
SELECT customer.cust_nbr,
customer.natl_acct_sgrp_nbr,
customer.sls_terr_nbr,
business_alignment.align_terr_id,
align,
customer.cust_acct_crte_dt,
customer.edi_cust_nbr,
contact.cont_co_nm,
contact_address.ADR_LN1_DESC,
contact_address.ADR_LN2_DESC,
contact_address.CITY_NM,
contact_address.ST_CD,
contact_address.PSTL_CD,
contact.ph_nbr_area_cd,
contact.ph_nbr,
contact.cont_first_nm,
contact.cont_mdl_nm,
contact.cont_last_nm,
customer.dom_lst_shp_dt,
customer.dom_adr_amt,
customer.cust_cur_stat_cd,
customer_segment.enti_nbr,
customer_segment.enti_seg_cd,
customer.purge_dt,
Case
When business_alignment.prim_cvge_cd = 'Y' AND business_alignment.align_terr_id LIKE '1-2%'
Then 'IS'
When business_alignment.prim_cvge_cd = 'N' AND business_alignment.align_terr_id LIKE '1-2%'
Then 'IS'
When business_alignment.prim_cvge_cd = 'Y' AND business_alignment.align_terr_id LIKE '1-1%'
Then 'FS'
When business_alignment.prim_cvge_cd = 'N' AND business_alignment.align_terr_id LIKE '1-1%'
Then 'FS'
When business_alignment.prim_cvge_cd = 'Y' AND business_alignment.align_terr_id LIKE '9%'
Then 'MK'
When business_alignment.prim_cvge_cd = 'N' AND business_alignment.align_terr_id LIKE '9%'
Then 'MK'
end as align,
ROW_NUMBER() OVER(partition by customer.cust_nbr ORDER BY customer.cust_nbr, customer.cust_acct_crte_dt) AS RN
FROM customer, contact_address, customer_segment, business_alignment
WHERE customer.natl_acct_nbr='63500'
AND customer.cust_nbr = customer_segment.cust_acct_nbr
AND customer.cust_nbr = contact.cust_nbr
AND business_alignment.cust_nbr = customer.cust_nbr
AND customer.cust_nbr = contact_address.CUST_NBR
AND contact.cont_role_type_cd='ps'
AND business_alignment.exp_dt = date '2525-05-31'
AND (align = 'IS' OR align = 'FS' OR align = 'MK')) AS T
WHERE T.RN = 1
|