Queries not working when joined

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I have two queries that work correctly when run separately.
When I try to use them as subqueries to join the two together, they stop working.
Here's the SQL:-
Monthly
Code:
SELECT Month([contactDatetime]) AS Month_Number, Count(ext_Contacts.clientId) AS CountOfclientId, ext_Contacts.formOfContact, Year([contactDatetime]) AS [Year]
FROM ext_Contacts
GROUP BY Month([contactDatetime]), ext_Contacts.formOfContact, Year([contactDatetime])
HAVING (((Month([contactDatetime]))=[Month Number]) AND ((ext_Contacts.formOfContact)<>"Telephone" And (ext_Contacts.formOfContact)<>"Email" And (ext_Contacts.formOfContact)<>"Letter") AND ((Year([contactDatetime]))=[Year]));
This returns zero results for May which is expected.


Year-to-Date
Code:
SELECT Count(ext_Contacts.clientId) AS CountOfclientId, ext_Contacts.formOfContact, Year([contactDatetime]) AS [Year]
FROM ext_Contacts
GROUP BY ext_Contacts.formOfContact, Year([contactDatetime])
HAVING (((ext_Contacts.formOfContact)<>"Telephone" And (ext_Contacts.formOfContact)<>"Email" And (ext_Contacts.formOfContact)<>"Letter") AND ((Year([contactDatetime]))=[Year]));
This returns 1 result for this year which is correct.


Now, when I amalgamate the two queries (which gives the following SQL)
Code:
SELECT qry_Monthly_General_F2F.CountOfclientId, qry_YTD_General_F2F.CountOfclientId
FROM qry_Monthly_General_F2F, qry_YTD_General_F2F;
I get no results. The 1 from the year-to-date section has disappeared.

I know the two queries are not linked but I haven't linked other queries of similar design and they work OK.

Anyone any ideas?
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Can't use a UNION unfortunately, the queries have slightly different outputs and a different number of columns.
 
Upvote 0
Why not change the month one to accept start month and end month, and ditch the year to date

(((Month([contactDatetime]))=>[START Month Number]) AND (((Month([contactDatetime]))<=[END Month Number])
 
Upvote 0
Can't use a UNION unfortunately, the queries have slightly different outputs and a different number of columns.

I have overcome this in UNON queries by creating Null or "" fields in one of the queries so they DO match.

eg I have a tbllive and an tblarchive , the archive tbl has a field tblarchive.archive_date field that the live does not, otherwise same structure.

A query on both is made with a Union of 2 similar queries using "" for the archive_date for the tbllive query, then a JOIN on the now matching queries.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top