Query to Subtract two queries

ambrn14

Registered User.
Local time
Yesterday, 19:57
Joined
Mar 17, 2011
Messages
43
I have two queries and give me a result for each that I want to subtract. In the SQL world, this is not as easy as it sounds. I am about the drive myself insane. :confused: And the info. that I have found on the internet has been no help.

Here are the two queries-
TotalEDCalled=
SELECT Month([CodeStrokeDate]) & "/" & Year([CodeStrokeDate]) AS [Month/Year], Count(tblCodeStrokePts.CodeStrokeDate) AS [Total ED Called]
FROM tblCodeStrokePts
WHERE (((tblCodeStrokePts.CodeStrokeDate) Between [Forms]![frmQuerySwitchboard]![txtStartdate] And [Forms]![frmQuerySwitchboard]![txtEnddate]))
GROUP BY Month([CodeStrokeDate]) & "/" & Year([CodeStrokeDate]), Year([CodeStrokeDate]), Month([CodeStrokeDate]), tblCodeStrokePts.InHouseCS, tblCodeStrokePts.IVtPADripAndShip
HAVING (((tblCodeStrokePts.InHouseCS)="No" Or (tblCodeStrokePts.InHouseCS) Is Null Or (tblCodeStrokePts.InHouseCS)="N") AND ((tblCodeStrokePts.IVtPADripAndShip)="No" Or (tblCodeStrokePts.IVtPADripAndShip) Is Null Or (tblCodeStrokePts.IVtPADripAndShip)="N"))
ORDER BY Year([CodeStrokeDate]), Month([CodeStrokeDate]);

And

TotalEDCanceled=
SELECT Month([CodeStrokeDate]) & "/" & Year([CodeStrokeDate]) AS [Month/Year], Count(tblCodeStrokePts.CodeStrokeDate) AS [Total ED Canceled]
FROM tblCodeStrokePts
WHERE (((tblCodeStrokePts.Canceled)="Y") AND ((tblCodeStrokePts.CodeStrokeDate) Between [Forms]![frmQuerySwitchboard]![txtStartdate] And [Forms]![frmQuerySwitchboard]![txtEnddate]))
GROUP BY Month([CodeStrokeDate]) & "/" & Year([CodeStrokeDate]), Year([CodeStrokeDate]), Month([CodeStrokeDate]), tblCodeStrokePts.InHouseCS
HAVING (((tblCodeStrokePts.InHouseCS)="No" Or (tblCodeStrokePts.InHouseCS) Is Null Or (tblCodeStrokePts.InHouseCS)="N"))
ORDER BY Year([CodeStrokeDate]), Month([CodeStrokeDate]);

I want to subtract TotalEDCanceled from TotalEDCalled to give me TotalEDCompleted.

I would greatly appreciate anyone's help. Thank you!
 
If you have matching data (by month/year) for both counts, you can create a third query that joins your 2 queries by the [month/year] field and do the subtraction there.

Now if you have month/year records in one query but not in the other, things get a little more complicated but there are ways of handling that as well.
 
I have matching data (by month/year) for both counts. A third query that joins my 2 queries by the [month/year] field and do the subtractions is the query that I am having trouble with.

Can you help me with the SQL for that type of query?
 
Something along these lines:

SELECT TotalEDCalled.[Total ED Called]-TotalEDCanceled.[Total ED Canceled] As EDDifference
FROM TotalEDCalled INNER JOIN TotalEDCanceled ON TotalEDCalled.[month/year]=TotalEDCanceled.[month/year]
 

Users who are viewing this thread

Back
Top Bottom