Hi there i'm sure there is wizard some where that knows where i am going wrong with my exists subquery.
Basically, i started out with the following query, that provides a list of 55 MPRs and there associated MeterpointAQs, when i input NOvembers dates, as per below:
The results of this would look like
MPR Meter point AQ
123456 1000
789123 2500
456879 4000....
Based on these results, I'm then trying to catagorise these 55 MPRs based on the MeterpointAQ being between the [LowValue] and [HighValue] in table [A12-VolCat_Tbl_2] and display the results as follows:
Vol_Band Count of MPRs Sum of MeterPointAQ
0-1000 1 1000
1001-5000 2 6500....
The code i have in place is:
What seams to be happening though, is that instead of just querying and grouping the 55 MPRs from the sub query it instead puts the total MPRs and the total AQ from the sub query against every category, regardless of what dates are input like so:
Vol_Band Count of MPRs Sum of MeterPointAQ
0-1000 800 15486
1001-5000 800 15486
5001-10001 800 15486
I have also tried including the WHERE criteria in the outer query, but then this throws back an answer of 64, which basically implies that it is ignoring the sub query?
I hope this makes sense to someone and they can let me know where i am going wrong??
OOch
Basically, i started out with the following query, that provides a list of 55 MPRs and there associated MeterpointAQs, when i input NOvembers dates, as per below:
Code:
[COLOR=red]
SELECT [A02-GAS_YTD_Losses].MPR, Sum([A02-GAS_YTD_Losses]. [Meter point AQ]) AS [SumOfMeter point AQ]
FROM [A02-GAS_YTD_Losses] LEFT JOIN [A05-Md_Migrated_MPR_Tbl] ON [A02-GAS_YTD_Losses].MPR = [A05-Md_Migrated_MPR_Tbl].MigratedMPR
WHERE (([A05-Md_Migrated_MPR_Tbl].MigratedMPR Is Null) AND ([A02-GAS_YTD_Losses].[Ceased date] Between [Start Date] And [End Date]) AND ([A02-GAS_YTD_Losses].NA_Ctrt_STATUS Like "*tar*"))
GROUP BY [A02-GAS_YTD_Losses].MPR;
[/color]
The results of this would look like
MPR Meter point AQ
123456 1000
789123 2500
456879 4000....
Based on these results, I'm then trying to catagorise these 55 MPRs based on the MeterpointAQ being between the [LowValue] and [HighValue] in table [A12-VolCat_Tbl_2] and display the results as follows:
Vol_Band Count of MPRs Sum of MeterPointAQ
0-1000 1 1000
1001-5000 2 6500....
The code i have in place is:
Code:
[COLOR=green]
SELECT [A12-VolCat_Tbl_2].Vol_Band, Count([A02-GAS_YTD_Losses].MPR) AS CountOfMPR, Sum([A02-GAS_YTD_Losses].[Meter point AQ]) AS [SumOfMeter point AQ]
FROM [A02-GAS_YTD_Losses], [A12-VolCat_Tbl_2]
WHERE EXISTS
[COLOR=red](SELECT [A02-GAS_YTD_Losses].MPR, Sum([A02-GAS_YTD_Losses].[Meter point AQ]) AS [SumOfMeter point AQ]
FROM [A02-GAS_YTD_Losses] LEFT JOIN [A05-Md_Migrated_MPR_Tbl] ON [A02-GAS_YTD_Losses].MPR = [A05-Md_Migrated_MPR_Tbl].MigratedMPR
WHERE (([A05-Md_Migrated_MPR_Tbl].MigratedMPR Is Null) AND ([A02-GAS_YTD_Losses].[Ceased date] Between [Start Date] And [End Date])
AND ([A02-GAS_YTD_Losses].NA_Ctrt_STATUS Like "*tar*"))
GROUP BY [A02-GAS_YTD_Losses].MPR)[/color]
GROUP BY [A12-VolCat_Tbl_2].Vol_Band;
[/color]
What seams to be happening though, is that instead of just querying and grouping the 55 MPRs from the sub query it instead puts the total MPRs and the total AQ from the sub query against every category, regardless of what dates are input like so:
Vol_Band Count of MPRs Sum of MeterPointAQ
0-1000 800 15486
1001-5000 800 15486
5001-10001 800 15486
I have also tried including the WHERE criteria in the outer query, but then this throws back an answer of 64, which basically implies that it is ignoring the sub query?
I hope this makes sense to someone and they can let me know where i am going wrong??
OOch