Question : SQL DISTINCT still returns dups

I am getting multiple returns for each customer.cust_nbr, even if I use DISTRICT(customer.cust_nbr). The query should return 400 but returning 1000.

Can I get only 1 distinct cust_nbr? Thanks!
Beth
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
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

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')

Answer : SQL DISTINCT still returns dups

Try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
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
Random Solutions  
 
programming4us programming4us