Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning "Distinct" results from multiple tables

Status
Not open for further replies.

sps533

Programmer
Jan 2, 2007
2
US
Well the best way of explaining I can think of is using an example...

Let's say I have two tables:
STORE_A_CUSTOMER_TRAN
CUSTOMER_NO TRAN_TYPE_CD ...
111 Z ...
111 Z ...
222 Z ...
...

STORE_B_CUSTOMER_TRAN
CUSTOMER_NO TRAN_TYPE_CD ...
111 Y ...
222 Y ...
333 Y ...
...

What I am trying to get is a distinct or grouped by list of all of the customer_no's from both tables. So if customer 1 has transactions stored in both tables while customer 2 has transactions only in the store B table, I want my results to only return "customer 1, customer 2"... So basically I am looking for something like:
SELECT A.CUSTOMER_NO, B.CUSTOMER_NO
FROM STORE_A_CUSTOMER_TRAN A,
STORE_B_CUSTOMER_TRAN B
WHERE A.TRAN_TYPE_CD = 'Z'
AND B.TRAN_TYPE_CD = 'Y'
GROUP BY (A.CUSTOMER_NO, B.CUSTOMER_NO);

So using my example tables above I want my results to be:
CUSTOMER_NO
111
222
333

This is more-or-less the method behind my madness. A customer may exist in one, or both tables. Essentially I've got two separate queries, I'm just looking for a way group their results. Any assistance would be GREATLY appreciated :)

Thanks!
 
Code:
select customer_no
  from store_a_customer_tran
 where tran_type_cd = 'Z'
union
select customer_no
  from store_b_customer_tran
 where tran_type_cd = 'Y'
 
Wow, that Union does the trick... shame on me for not remembering its purpose :p

Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top