Query total by month (1 Viewer)

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
I have a query that I need to total by each month of the year. Field names and types are as follows

ID (PK)
Month (Date/Time)
PoliceResponse (1 or 0)
TerminalCheckTrue (1 or 0)
RampCheckTrue (1 or 0)
AOACheckTrue (1 or 0)
TotalTerminalRampAOA (TerminalCheckTrue + RampCheckTrue + AOACheckTrue)
TotalChecks (PoliceResponse+TerminalCheckTrue + RampCheckTrue + AOACheckTrue)


Now, I need to just get the total of each of these fields per month for the entire year. So it would look like

Jan, MonthlyPoliceResponse, MonthlyTerminalChecks, etc...
Feb, MonthlyPoliceResponse, MonthlyTerminalChecks, etc...
Mar, MonthlyPoliceResponse, MonthlyTerminalChecks, etc...

Below is a sample from the query. I guess Im just not sure where to start since there are multiple months the same value, but i need them to total up..

Code:
SELECT DISTINCTROW tblBlotter.ID, Format$([tblBlotter].[EntryDate],'mmmm yyyy') AS [Month], Sum(IIf([CKPoliceResponse],1,0)) AS PoliceResponseTrue, Sum(IIf([CKTerminalCheck],1,0)) AS TerminalCheckTrue, Sum(IIf([CKRampCheck],1,0)) AS RampCheckTrue, Sum(IIf([CKAOACheck],1,0)) AS AOACheckTrue, ([TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalTerminalRampAOA, ([PoliceResponseTrue]+[TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalChecks
FROM tblBlotter
GROUP BY tblBlotter.ID, Format$([tblBlotter].[EntryDate],'mmmm yyyy'), Year([tblBlotter].[EntryDate])*12+DatePart('m',[tblBlotter].[EntryDate])-1, Year([tblBlotter].[EntryTime])*12+DatePart('m',[tblBlotter].[EntryTime])-1;
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Also, I tried to make a crosstab query based off this query but it would only let me choose 3 columns...
 

jdraw

Super Moderator
Staff member
Local time
Today, 12:18
Joined
Jan 23, 2006
Messages
15,379
What exactly does this mean?
RampCheckTrue (1 or 0)

Is RampCheckTrue a Boolean Yes/No data type?
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Yes, It was a checkbox field but couldnt total it. So I have it converted to 1 for true and 0 for false
 

JHB

Have been here a while
Local time
Today, 18:18
Joined
Jun 17, 2012
Messages
7,732
Post some sample data in an Excel sheet, or in a database.
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
The distinct rows most likely come from the fact that you have the ID in the select.
Code:
SELECT [COLOR="red"]DISTINCTROW tblBlotter.ID,[/COLOR] Format$([tblBlotter].[EntryDate],'mmmm yyyy') AS [Month], Sum(IIf([CKPoliceResponse],1,0)) AS PoliceResponseTrue, Sum(IIf([CKTerminalCheck],1,0)) AS TerminalCheckTrue, Sum(IIf([CKRampCheck],1,0)) AS RampCheckTrue, Sum(IIf([CKAOACheck],1,0)) AS AOACheckTrue, ([TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalTerminalRampAOA, ([PoliceResponseTrue]+[TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalChecks
FROM tblBlotter
GROUP BY [COLOR="Red"]tblBlotter.ID,[/COLOR] Format$([tblBlotter].[EntryDate],'mmmm yyyy'), Year([tblBlotter].[EntryDate])*12+DatePart('m',[tblBlotter].[EntryDate])-1, Year([tblBlotter].[EntryTime])*12+DatePart('m',[tblBlotter].[EntryTime])-1;
Try removing the red bits and if that helps you along.
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Here is a sample DB of the table and query. The excel document has the formula and data I am trying to match.

So the totals of each type of Response/Check for each month.
Then the Monthly % Change, which is I'm guessing would be written something like
((CurrentMonth-PreviousMonth)/PreviousMonth*100). Except somehow these months need to be written statically I believe - so each month that advances, I could retain previous months data.
 

Attachments

  • Database2.accdb
    960 KB · Views: 145
  • 2013 Responses and Checks for 316.xls
    29 KB · Views: 158

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Just saw the response from namliam. Let me look at this and check the results..
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Looks good.. Thanks.
How do you suppose I can get the final calculation I need

So the totals of each type of Response/Check for each month.
Then the Monthly % Change, which is I'm guessing would be written something like
((CurrentMonth-PreviousMonth)/PreviousMonth*100). Except somehow these months need to be written statically I believe - so each month that advances, I could retain previous months data.
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
You can do this in 2 queries:
1) Save this as Quer1
Code:
SELECT Year([tblBlotter].[EntryDate]) AS [Year], Month([tblBlotter].[EntryDate]) AS [Month], Sum(IIf([CKPoliceResponse],1,0)) AS PoliceResponseTrue, Sum(IIf([CKTerminalCheck],1,0)) AS TerminalCheckTrue, Sum(IIf([CKRampCheck],1,0)) AS RampCheckTrue, Sum(IIf([CKAOACheck],1,0)) AS AOACheckTrue, ([TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalTerminalRampAOA, ([PoliceResponseTrue]+[TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalChecks, Year(DateAdd("m",-1,[tblBlotter].[EntryDate])) AS PrevYear, Month(DateAdd("m",-1,[tblBlotter].[EntryDate])) AS PrevMonth
FROM tblBlotter
GROUP BY Year([tblBlotter].[EntryDate]), Month([tblBlotter].[EntryDate]), Year([tblBlotter].[EntryDate])*12+DatePart('m',[tblBlotter].[EntryDate])-1, Year([tblBlotter].[EntryTime])*12+DatePart('m',[tblBlotter].[EntryTime])-1, Year(DateAdd("m",-1,[tblBlotter].[EntryDate])), Month(DateAdd("m",-1,[tblBlotter].[EntryDate]));

2) Save this as Query2
Code:
SELECT Year([tblBlotter].[EntryDate]) AS [Year], Month([tblBlotter].[EntryDate]) AS [Month], Sum(IIf([CKPoliceResponse],1,0)) AS PoliceResponseTrue, Sum(IIf([CKTerminalCheck],1,0)) AS TerminalCheckTrue, Sum(IIf([CKRampCheck],1,0)) AS RampCheckTrue, Sum(IIf([CKAOACheck],1,0)) AS AOACheckTrue, ([TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalTerminalRampAOA, ([PoliceResponseTrue]+[TerminalCheckTrue]+[RampCheckTrue]+[AOACheckTrue]) AS TotalChecks, Year(DateAdd("m",-1,[tblBlotter].[EntryDate])) AS PrevYear, Month(DateAdd("m",-1,[tblBlotter].[EntryDate])) AS PrevMonth
FROM tblBlotter
GROUP BY Year([tblBlotter].[EntryDate]), Month([tblBlotter].[EntryDate]), Year([tblBlotter].[EntryDate])*12+DatePart('m',[tblBlotter].[EntryDate])-1, Year([tblBlotter].[EntryTime])*12+DatePart('m',[tblBlotter].[EntryTime])-1, Year(DateAdd("m",-1,[tblBlotter].[EntryDate])), Month(DateAdd("m",-1,[tblBlotter].[EntryDate]));
NOTICE it is EXACTLY the same

3) make a 3rd query:
Code:
SELECT Query1.*, Query2.*
FROM Query1 LEFT JOIN Query2 ON (Query1.PrevMonth = Query2.Month) AND (Query1.PrevYear = Query2.Year);
You can make any calculation you like :)
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
The Grouping/Counts are great! Thanks!
My formula could stand a bit of help if you can though.
It keeps prompting me to enter the values when I run the report.

Code:
Expr1: (([Month]-[qryBlotter1]![PrevMonth])/[qryBlotter1]![PrevMonth]*100)
 

spikepl

Eledittingent Beliped
Local time
Today, 18:18
Joined
Nov 3, 2010
Messages
6,142
When I count yes-responses for a Boolean then instead of

Sum(IIf([CKPoliceResponse],1,0))

I use

-Sum([CKPoliceResponse])
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
why the Flieping fliep would you calculate with the actual months, that doesnt may ANY sence what so ever.
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
lol. Sorry thats not what I meant. The months calculate correctly.
I just wanted to add one more expression at the end of this query to complete it.

So in effect, if this is Month2, I need
MonthlyChange=((Month2-Month1)/Month1*100)
and
MonthlyChange=((Month3-Month2)/Month2*100)

Always... No matter what the current month is.

I'm just not sure where to begin, given I now have 3 queries :/
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
Well what values do you have in a single line? The two individual months and their values

So with each of the lines in the 3rd query you can do what ever calculation you want between the two months.
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
Futhermore your database you linked in your other thread...
http://www.access-programmers.co.uk/forums/showthread.php?t=260648

you didnt build the KEY solution of the third query...
Code:
SELECT qryBlotter1.*, qryBlotter2.*
FROM qryBlotter1 LEFT JOIN qryBlotter2 ON (qryBlotter1.PrevMonth = qryBlotter2.Month) AND (qryBlotter1.PrevYear = qryBlotter2.Year);
Perhaps if you do, you may be able to see the solution should be a lot easier than you seem to believe....
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
Oops.
I did, just forgot I had another query there bc I had it named "qryBlotterTotals"
(My naming conventions are weak) This is the one you are referring to.

Code:
SELECT qryBlotter1.*, qryBlotter2.*
FROM qryBlotter1 LEFT JOIN qryBlotter2 ON (qryBlotter1.PrevYear = qryBlotter2.Year) AND (qryBlotter1.PrevMonth = qryBlotter2.Month);
 

foshizzle

Registered User.
Local time
Today, 12:18
Joined
Nov 27, 2013
Messages
277
So given this, I think the query should be something like

Code:
(([qryBlotter1]![Month] - [qryBlotter1]![PrevMonth] )/ [qryBlotter1]![PrevMonth] *100)

But thats not right. And further, I just dont get how I would use both the year and month to be sure the correct information correlates.. :(
 

namliam

The Mailman - AWF VIP
Local time
Today, 18:18
Joined
Aug 11, 2003
Messages
11,695
You dont calculate using the months, you calculate using the fields you want to know the difference between....

The queries I gave you already
1) calculate the previous month
2) correlate the information between the two...
using the join in the last query: (qryBlotter1.PrevYear = qryBlotter2.Year) AND (qryBlotter1.PrevMonth = qryBlotter2.Month)
 

Users who are viewing this thread

Top Bottom