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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Join two select statements and collecting data from both

Status
Not open for further replies.

ms901Boss

Programmer
Mar 19, 2021
16
0
0
US
I have two queries that have data that I need in one report.

Query a1:
Code:
Select fname, lname,state
from contact
where state = 'TN'

Ouput data:
Code:
fname    lname    state
Kim	  Smith	  TN
Jim       Jones   TN
Sarah     Lewis   FL
Joe       Wills   TN

Query a2:
Code:
Select fname, lname, state
from customer
where state = 'TN'

Ouput data:
Code:
 fname    lname    state
Kim	  Smith	   TN
Jim       Jones    TN
John      LONG     CA
Tim       Brown    TN

Here is what I did with the two quieres

Code:
select a1.fname, al.lname,a2.state
from contact 
(select fname, lname,state
from contact
where state = 'TN') as a1
left join 
(select fname, lname,state
from customer
where state = 'TN') as a2
on a1.state = a2.state
where state = 'TN'
The output I get is only the data from the left table.
Code:
The expected outcome:
Kim	  Smith	TN
Jim   Jones TN
Joe   Wills TN
Tim   Brown TN
How do I get data from both queries that match the main select statement criteria?

 
Your output data does not match your query.
state='TN' and you expect outputs with states FL and CA?

Please clarify what exactly it is you need.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
I agree with MakeItSo - it would help to know what you are trying to achieve.

Taken at its simplest, I think that this will achieve what you are asking for.

Select fname, lname, state
from contact
where state = 'TN'
UNION
Select fname, lname, state
from customer
where state = 'TN'

It might be useful to add the source table name to each select.

Select fname, lname, state, 'contact' as from_table
from contact
where state = 'TN'
UNION
Select fname, lname, state, 'customer' as from_table
from customer

Apologies is there are errors but I am doing this from memory and have no way of checking it, but hopefully it will point you in the right direction.
where state = 'TN'
 
try UNION
Code:
select fname, lname, state from 
(
  select fname, lname, state from contact
  union
  select fname, lname, state from customer
) as t
where state = 'TN'

Output:
Code:
FNAME           LNAME           STATE
Jim       	Jones     	TN
Joe       	Wills     	TN
Kim       	Smith     	TN
Tim       	Brown     	TN

 
mikrom, why does your code not produce duplicates?
 
Hi pjw001,
UNION does not produce duplicates, if you need duplicates use UNION ALL
 
I don't think I ever knew that. I wonder how many times this caused me problems.

I would suggest that UNION ALL is probably what OP wants.
 
Hi pjw001,
I thought, that probably UNION is needed, because ms901Boss wrote above this
Code:
The expected outcome:
Kim   Smith TN
Jim   Jones TN
Joe   Wills TN
Tim   Brown TN
and that is without duplicates.
 
Hi mikrom,

Yes, but he did say that he wanted data from both tables.

I guess we will have to wait and see what OP wants.
 
Hi pjw001,
Exactly, the boss who gave us this task should appear here to explain how the result should be. :)
 
I would also question why do we have the same data in more than one place (table)?
If Kim Smith from TN married and changes her last name, we would need to find her in many places to change her name.
It is also possible to have two (or more) people with the same First/Last name that live in the same state.
Then what [ponder]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
My guess is that this is the exact reason OP is trying to extract the data. i.e. so that the database can be tidied up (and possibly redesigned).

Oh, and Kim could be a he.
 
Nothing prevents HIM to change his name to his wife's when married [wiggle]
(or hiding from the law...)

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Probably, these are result tables from two queries and the task is to create third query from them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top