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!

Combining two queries into one 3

Status
Not open for further replies.

shaminda

Programmer
Jun 9, 2000
170
US
I have the following sql queries. How can I write 1 query so it returns only one result set?

select distinct fdShortNumber as PartNumber, count(fdShortNumber) as Quantity
from tbStation5
where fdAssemblyDate = @fdAssemblyDate AND fdAssemblyShift = @fdAssemblyShift
group by fdShortNumber

select distinct fdShortNumber as PartNumber, count(fdShortNumber) as Quantity
from [Production1].dbo.tbStation7
where fdAssemblyDate = @fdAssemblyDate AND fdAssemblyShift = @fdAssemblyShift
group by fdShortNumber
 
same FdShortNumber VALUES in both tables?

Please post examples of data in both tables, and what result you would expect based on your examples.
 

Here is the results from query1
PartNumber Quantity
70322 32
70323 113
70324 30

Here is the result from query2
PartNumber Quantity
71766 30
72003 84

I have attached sample data for both tables.

Here is what I expecting:
PartNumber Quantity
70322 32
70323 113
70324 30
71766 30
72003 84

 
 http://files.engineering.com/getfile.aspx?folder=72c4005c-498c-4b77-93a8-0f4339f9e0b5&file=Sample_Data.xlsx
Thanks SkipVought for your help. I did not know it was that easy.
 
It always depends on the case, what you need. In your case it was a union.

In general you can use a query as a table in outer SQL, by giving it a name, eg you have two queries, then you simply put them in brackets and add AS name, eg
Code:
(query1) as q1
and
Code:
(query2) as q2
That in itself is now just defining "tables" q1 and q2, or rather named queries, like views. Like any table or view name this now must be embedded in further query code. So now you can do whatever you could do with two tables named q1 and q1, eg join them:

Code:
Select * 
from [COLOR=#CC0000](query1) as q1[/color]
inner join [COLOR=#CC0000](query2) as q2[/color]
on q1.id= q2.id

For a union it's unnecessary to add the alias names first, as unions have no join matching clause, records are appended, not matched, but if you like straight forward writing of code you can apply this as strict rule and write:

Code:
Select * from [COLOR=#CC0000](query1) as q1[/color]
Union All
Select * from [COLOR=#CC0000](query2) as q2[/color]

This just has some redundancy, as you query all data from each query result and union that, you don't need the q1 and q2 names as reference. That's a sign you can simplify it and write it shorter as

Code:
query1
Union All
query2

But don't think it's always that simple, how to cope with subqueries depends on what operation you do with the query results, you don't always need brackets and an alias name, but it helps referencing the query result columns.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top