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

Joining two queries?

Status
Not open for further replies.

wyman

Technical User
Mar 11, 2002
6
CA
Hello,

I was wondering if it's possible to join the result of two queries together? I have one query that is used to calculate the revenue difference between projects that carryover to a new period. The other query filters out all projects that started exclusively in the current period. Would it be possible to merge these two query results together so that I get one query showing the total revenue for each project for the current month?

Your Guidance is much appreciated. Thanks in advance.
 

If you make sure there is a project Id in both of the queries {or another unique key}, you should easily be able to run a query off the query results.

SELECT * FROM qryRevDiff, qryCurrPeriod INNER JOIN qryRevDiff ON qryRevDiff.projectID = qryCurrPeriod.projectId;

Or something along those lines.

I hope this helps

 
Slight correction to the query posted by Lisbet.

SELECT * FROM qryRevDiff
INNER JOIN qryCurrPeriod
ON qryRevDiff.projectID = qryCurrPeriod.projectId;
Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions in the SQL Server forum. Many of the ideas apply to all forums.
 
Thanks,

However, I was not very explicit with my explanation of the problem. When I use the INNER JOIN, would it not look for values where the Project ID's from both query results are EQUAL? I am looking for a way to combine two queries that contain the information for different projects. So, in fact, each respective query will have different projects. Would there be way then to put these two queries back together?

For the record, I did try:
Code:
SELECT CarryoverInfo.*
FROM CarryoverInfo INNER JOIN UniqueRevenue ON CarryoverInfo.ProjectID = UniqueRevenue.ProjectID;

And, the resulting query did not give any results. Your help is greatly appreciated, and I apologize for being ambiguous.
 
Perhaps you want to use a UNION query. In order to perform a UNION query both queries need the same number of columns, in the same order and of the same data type.

Example: The two queries return the same row structure.

SELECT CarryoverInfo.*
FROM CarryoverInfo
UNION ALL
SELECT UniqueRevenue.*
FROM UniqueRevenue;

Example: 1st query returns two more columns than the 2nd but all other columns are in the same order.

SELECT CarryoverInfo.*
FROM CarryoverInfo
UNION ALL
SELECT UniqueRevenue.*, null, null
FROM UniqueRevenue;

Example: Queries have same columns but in different order.

SELECT c.*
FROM CarryoverInfo As c
UNION ALL
SELECT u.Col3, u.Col7, u.Col1, ..., u.ColN
FROM UniqueRevenue As u;

Let me know if the UNION query meets your needs. Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions in the SQL Server forum. Many of the ideas apply to all forums.
 
Terry,

You are a SAVIOR!!! The third example was exactly what I needed. I truly appreciate your help.

And thank you lisbet for helping out as well.

Much Thanks,

Wyman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top