Oddity in nested queries

RichardMGreen

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

I've got some nested queries that should work (famous last words).
All the sub-queries return the correct values (in this case 8 records for the month), but this is not showing on the final query which outputs an amalgamated total.

The SQL for the final query is:-
Code:
SELECT qry_Monthly_Client_Referrals_Total.CountOfClientId AS Monthly_Referrals, qry_Monthly_Client_Successful_Referrals_Total.CountOfClientId AS Monthly_Successful_Referrals, qry_YTD_Client_Referrals_Total.CountOfClientId AS YTD_Referrals, qry_YTD_Client_Successful_Referrals_Total.CountOfClientId AS YTD_Successful_Referrals
FROM qry_Monthly_Client_Successful_Referrals_Total, qry_Monthly_Client_Referrals_Total, qry_YTD_Client_Referrals_Total, qry_YTD_Client_Successful_Referrals_Total;

As you can see, there are no links (and none are necessary as far as I can make out) as it is just displaying totals already calculated in previous queries.

Can anyone make any suggestions as to why it's not pulliong through any data?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Not as far as I can see. I've built other queries using the same method and they all work fine.
It's just this one that's not pulling through the final data.
 
Last edited:
Upvote 0
Might have found the issue, now just need a solution.

It looks like it's only returning blanks when
qry_Monthly_Client_Successful_Referrals_Total.CountOfClientId AS Monthly_Successful_Referrals
is blank.
I thought it would have been zero or null but obviously not.
I'll try wrapping it in an iif/isnull statement and see how I get on.

::edit::
Well, that didn't work.
 
Last edited:
Upvote 0
Right, I've narrowed it down to this query (here's the SQL).
Code:
SELECT qry_Monthly_Client_Successful_Referalls.Month_Number, Count(qry_Monthly_Client_Successful_Referalls.ClientId) AS CountOfClientId
FROM qry_Monthly_Client_Successful_Referalls
GROUP BY qry_Monthly_Client_Successful_Referalls.Month_Number;

What I want to happen is that if qry_Monthly_Client_Successful_Referalls.ClientId is null then I get zero not a null return.
I've tried wrapping it in an iif/isnull statement but then I get error messages about aggregate functions.

Anyone any ideas?
 
Upvote 0
Right, solved it but there has to be a better way.
I've created a calendar table that lists all the months with month numbers and used that as a master table (left outer join) in the query.
I'm then restricting that to the month I want so I end with a zero (where necessary) that pulls through to the final query.

I don't know if there's a better way (I'm open to suggestions) but this works.
I still don't understand why it wiped out the other queries in the final one.
 
Upvote 0
Your first post lists a cartesian join of four queries. The last post doesn't seem remotely the same. I'm confused.

As far as "wrapping in isnull/nz goes", I *think* it would work this way:
Code:
SELECT 
    t.Month_Number, 
    Count([COLOR="RoyalBlue"]NZ(t.ClientId,0)[/COLOR]) AS CountOfClientId
FROM 
    qry_Monthly_Client_Successful_Referalls t
GROUP 
    t.Month_Number;
 
Upvote 0
The first post listed the outputs (or lack of them) from 4 other queries that were built on other queries and so on. It was simply a way to get all the data I needed into one output. The last bit I posted was my workaround for the query that was causing the problem.

Thanks for the SQL, can I drop that straight in to my Access query if I view SQL in design mode?
 
Upvote 0
Yes, I think you can drop it in in SQL view. You can use the same expression in Query Builder as an Expression:

SQL:
Count(NZ(t.ClientId,0)) AS CountOfClientId

Query Builder:
CountOfClientID: Count(NZ(t.ClientID,0))

But you need to select Expression rather than Count below in the field type.
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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