By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,506 Members | 2,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,506 IT Pros & Developers. It's quick & easy.

Reports

P: 19
Hi,

I have a table which contains the fields Empid, Empname, Amount, Date_check_rcvd. Now my question I want to generate a report which gives the sum of amount by month and by year thru the date of check recieved. I have a problem in writing the query where if I say sum(amount) and execute the query it gives an error saying " you tried to execute the query that does not include a specified expression Empid as part of aggregate function". I have very little experience in writing the queries. I just don't know what is wrong??can anyone help me out????
Feb 28 '07 #1
Share this Question
Share on Google+
21 Replies


MMcCarthy
Expert Mod 10K+
P: 14,534
Hi,

I have a table which contains the fields Empid, Empname, Amount, Date_check_rcvd. Now my question I want to generate a report which gives the sum of amount by month and by year thru the date of check recieved. I have a problem in writing the query where if I say sum(amount) and execute the query it gives an error saying " you tried to execute the query that does not include a specified expression Empid as part of aggregate function". I have very little experience in writing the queries. I just don't know what is wrong??can anyone help me out????
When using an aggregate query (query with things like Sum, Count, Avg, etc.) you must group by all fields not being aggregated. You can't do both Month and Year in the same query.

For instance...

For the Month:
Expand|Select|Wrap|Line Numbers
  1. SELECT Empid, Empname, Sum(Amount), Month(Date_check_rcvd)
  2. FROM TableName
  3. GROUP BY Empid, Empname, Month(Date_check_rcvd);
For the Year:
Expand|Select|Wrap|Line Numbers
  1. SELECT Empid, Empname, Sum(Amount), Year(Date_check_rcvd)
  2. FROM TableName
  3. GROUP BY Empid, Empname, Year(Date_check_rcvd);
Mary
Feb 28 '07 #2

ADezii
Expert 5K+
P: 8,599
Hi,

I have a table which contains the fields Empid, Empname, Amount, Date_check_rcvd. Now my question I want to generate a report which gives the sum of amount by month and by year thru the date of check recieved. I have a problem in writing the query where if I say sum(amount) and execute the query it gives an error saying " you tried to execute the query that does not include a specified expression Empid as part of aggregate function". I have very little experience in writing the queries. I just don't know what is wrong??can anyone help me out????
Assumptions:
__1 Table Name = Table1
__2 [Empid] AutoNumber (Primary Key)
__3 [Empname] (TEXT 50)
__4 [Amount] (CURRENCY)
__5 [Date_check_rcvd] (DATE/TIME)
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(Table1.Amount) AS SumOfAmount
  2. SELECT Table1.Empname AS Employee, Sum(Table1.Amount) AS [Total Amount], Year([Date_check_rcvd]) AS Year
  3. FROM Table1
  4. GROUP BY Table1.Empname, Year([Date_check_rcvd])
  5. PIVOT Format([Date_check_rcvd],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
Feb 28 '07 #3

P: 19
Thank you for the reply.

I am sorry in my previous post I forgot to mention other field i.e., Memosent which of data type date/time. Now when I run the query you have given me in the post it prompts me the message saying " enter the parameter value Memosent". When I click ok on the message then the report opens.
Feb 28 '07 #4

P: 19
My reply is for the post given by mmccarthy
Feb 28 '07 #5

MMcCarthy
Expert Mod 10K+
P: 14,534
Thank you for the reply.

I am sorry in my previous post I forgot to mention other field i.e., Memosent which of data type date/time. Now when I run the query you have given me in the post it prompts me the message saying " enter the parameter value Memosent". When I click ok on the message then the report opens.
What is this field and why do you need to include it. ADezii's example is better than mine BTW.

Mary
Feb 28 '07 #6

P: 19
I even tried the code given by ADezii but it prompts the same msg saying "enter the parameter value date_check_rcvd" and also when I run the report it asks me to enter the amount,date_check_rcvd
Feb 28 '07 #7

P: 19
The field is nothing but the date when the check was sent.
Feb 28 '07 #8

MMcCarthy
Expert Mod 10K+
P: 14,534
The field is nothing but the date when the check was sent.
Can you leave it out as the date_check_rcvd is the date being used for the calculation. Another date like the memosent date will mess up all the figures.

Mary
Feb 28 '07 #9

P: 19
Can you leave it out as the date_check_rcvd is the date being used for the calculation. Another date like the memosent date will mess up all the figures.

Mary
I don't think I can do that because I want all the fields in the report.
Feb 28 '07 #10

MMcCarthy
Expert Mod 10K+
P: 14,534
Try this revised version of ADezii's code.

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(amount) AS TotalAmount
  2. SELECT Empname AS Employee, Sum(amount) AS TotalAmount, Year([date_check_rcvd]) AS Year
  3. FROM TableName
  4. GROUP BY Empname, Year([date_check_rcvd])
  5. PIVOT Format([date_check_rcvd],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  6.  
Mary
Feb 28 '07 #11

MMcCarthy
Expert Mod 10K+
P: 14,534
I don't think I can do that because I want all the fields in the report.
To explain ...

We are totaling the Amount per Employee based on the Month and Year of the date the check was received. If another date is introduced then you can't calculate the figure as it will be broken down over the other date.

Mary
Feb 28 '07 #12

P: 19
Try this revised version of ADezii's code.

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(amount) AS TotalAmount
  2. SELECT Empname AS Employee, Sum(amount) AS TotalAmount, Year([date_check_rcvd]) AS Year
  3. FROM TableName
  4. GROUP BY Empname, Year([date_check_rcvd])
  5. PIVOT Format([date_check_rcvd],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  6.  
Mary
when I run the query it gives an error saying duplicate output alias "TotalAmount"
Feb 28 '07 #13

P: 19
To explain ...

We are totaling the Amount per Employee based on the Month and Year of the date the check was received. If another date is introduced then you can't calculate the figure as it will be broken down over the other date.

Mary
So, Do I need to remove the field in the query?
Feb 28 '07 #14

MMcCarthy
Expert Mod 10K+
P: 14,534
So, Do I need to remove the field in the query?
Yes.

And the error on ADezii's was my fault. Try this...
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(amount) AS SumOfAmount
  2. SELECT Empname AS Employee, Sum(amount) AS TotalAmount, Year([date_check_rcvd]) AS Year
  3. FROM TableName
  4. GROUP BY Empname, Year([date_check_rcvd])
  5. PIVOT Format([date_check_rcvd],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  6.  
Mary
Feb 28 '07 #15

P: 19
Yes.

And the error on ADezii's was my fault. Try this...
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Sum(amount) AS SumOfAmount
  2. SELECT Empname AS Employee, Sum(amount) AS TotalAmount, Year([date_check_rcvd]) AS Year
  3. FROM TableName
  4. GROUP BY Empname, Year([date_check_rcvd])
  5. PIVOT Format([date_check_rcvd],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  6.  
Mary
As you said I have removed the field and now when I execute the query it was successful. I have created the report based on this query I mean in the report's recordsource I have given the name of this query. Now when I run the report it prompts me asking for the amount and date_check_rcvd.

And one more thing I want the report in such a way that it should prompt me saying that which month for monthly repports and which year for yeraly reports
Feb 28 '07 #16

P: 19
Is there anything that I can do with the reports? I tried in various ways but could not make it up.
Feb 28 '07 #17

MMcCarthy
Expert Mod 10K+
P: 14,534
Is there anything that I can do with the reports? I tried in various ways but could not make it up.
If that's the case then use my code rather than ADezii's and in the report open code add the criteria based on a couple of combo boxes set up with year and month number bound with month showing.

For monthly reports:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DoCmd.OpenReport "ReportName", , , "Month(Date_check_rcvd)=" & Me.Combo1 & _
  3.    " AND Year(Date_check_rcvd)=" & Me.Combo2
Mar 1 '07 #18

P: 19
If that's the case then use my code rather than ADezii's and in the report open code add the criteria based on a couple of combo boxes set up with year and month number bound with month showing.

For monthly reports:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DoCmd.OpenReport "ReportName", , , "Month(Date_check_rcvd)=" & Me.Combo1 & _
  3.    " AND Year(Date_check_rcvd)=" & Me.Combo2
I don't think I can put combo box for the year because the reports are for future use. What if we have two textboxes for the datefrom and dateto and have 2 command buttons for month and year. So, once we enter say 1/1/2003 - 1/31/2003 and click on the month button it should give the report for that month calculating the sum of amount and same is for the year. Here my problem is I am able to generate the report monthly and yearly but I am not able to calculate the sum of amount neither monthly nor yearly. Can you help me out in this??
Mar 1 '07 #19

NeoPa
Expert Mod 15k+
P: 31,186
Can you please try to clarify in your own mind what it is you want.
Having reviewed this thread it seems that you're introducing new and important information periodically when an answer doesn't fit your situation. This is because you haven't shared the information to start with.
It also seems that you're reluctant even to try out the answers supplied and want to introduce your own, quite incompatible, ideas along the way. This is not reasonable and makes it so much harder for our experts to help you. It's hard enough to answer complicated questions remotely as it is, never mind a question where the original poster hasn't shared the necessary information and also changes the requirement on the fly.
I will leave this thread open in case anyone feels the need to reply, but you can consider this thread closed as far as the question itself is concerned.
I suggest you get your information together and repost the question clearly, and hope that someone dares to start helping you on it.

MODERATOR.
Mar 2 '07 #20

P: 19
Can you please try to clarify in your own mind what it is you want.
Having reviewed this thread it seems that you're introducing new and important information periodically when an answer doesn't fit your situation. This is because you haven't shared the information to start with.
It also seems that you're reluctant even to try out the answers supplied and want to introduce your own, quite incompatible, ideas along the way. This is not reasonable and makes it so much harder for our experts to help you. It's hard enough to answer complicated questions remotely as it is, never mind a question where the original poster hasn't shared the necessary information and also changes the requirement on the fly.
I will leave this thread open in case anyone feels the need to reply, but you can consider this thread closed as far as the question itself is concerned.
I suggest you get your information together and repost the question clearly, and hope that someone dares to start helping you on it.

MODERATOR.
Thank you for ur comments. I have got the reports to be generated.
Mar 5 '07 #21

NeoPa
Expert Mod 15k+
P: 31,186
Though I may sound critical, I'm nevertheless pleased you managed to resolve your issues.
Mar 5 '07 #22

Post your reply

Sign in to post your reply or Sign up for a free account.