Difference within subgroup

R

Ray S.

I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes

Any ideas how to approach solution?
 
M

Marshall Barton

Ray said:
I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes


This kind of aggregation/summary is best done by using a
subreport based on a query that calculates the differences.

Let's say the name of the query for your main report is
qryMain, then the subreport's record source query could be
something like:

SELECT qryMain.Year, Sum(qryMain.amount)
- (SELECT Sum(X.amount)
FROM qryMain As X
WHERE X.yearfield = qryMain.yearfield - 1)
FROM qryMain
 
R

Ray S.

OK Marshall

Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...
 
M

Marshall Barton

Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr
 
R

Ray S.

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?

Marshall Barton said:
Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr
--
Marsh
MVP [MS Access]

Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...
 
M

Marshall Barton

I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
 
R

Ray S.

by "item" I meant "record". Each record has a project type, project, and
expense amount. In the crosstab query I used project type and project as my
row headings, the year field as a column heading (to separate the years out),
and the sum of the expense amounts as my value...It looks pretty good...I
want to calculate percentage increases or decreases, but was having that null
problem...I'll try the Nz function

Marshall Barton said:
I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
--
Marsh
MVP [MS Access]

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?
 
M

Marshall Barton

Ahhh, two row headings. In that case the non-crosstab query
would be more like:

SELECT qryMain.yr, qryMain.ProjectType, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project
AND X.ProjectType = qryMain.ProjectType) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.ProjectType, qryMain.Project
--
Marsh
MVP [MS Access]

by "item" I meant "record". Each record has a project type, project, and
expense amount. In the crosstab query I used project type and project as my
row headings, the year field as a column heading (to separate the years out),
and the sum of the expense amounts as my value...It looks pretty good...I
want to calculate percentage increases or decreases, but was having that null
problem...I'll try the Nz function

Marshall Barton said:
I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
--
Marsh
MVP [MS Access]

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?

:

Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr


Ray S. wrote:
Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...

:

Ray S. wrote:

I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes


This kind of aggregation/summary is best done by using a
subreport based on a query that calculates the differences.

Let's say the name of the query for your main report is
qryMain, then the subreport's record source query could be
something like:

SELECT qryMain.Year, Sum(qryMain.amount)
- (SELECT Sum(X.amount)
FROM qryMain As X
WHERE X.yearfield = qryMain.yearfield - 1)
FROM qryMain
 
R

Ray S.

Great, what criteria would you use to decide whether or not to use a crosstab
query?

Marshall Barton said:
Ahhh, two row headings. In that case the non-crosstab query
would be more like:

SELECT qryMain.yr, qryMain.ProjectType, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project
AND X.ProjectType = qryMain.ProjectType) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.ProjectType, qryMain.Project
--
Marsh
MVP [MS Access]

by "item" I meant "record". Each record has a project type, project, and
expense amount. In the crosstab query I used project type and project as my
row headings, the year field as a column heading (to separate the years out),
and the sum of the expense amounts as my value...It looks pretty good...I
want to calculate percentage increases or decreases, but was having that null
problem...I'll try the Nz function

Marshall Barton said:
I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
--
Marsh
MVP [MS Access]


Ray S. wrote:

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?

:

Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr


Ray S. wrote:
Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...

:

Ray S. wrote:

I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes


This kind of aggregation/summary is best done by using a
subreport based on a query that calculates the differences.

Let's say the name of the query for your main report is
qryMain, then the subreport's record source query could be
something like:

SELECT qryMain.Year, Sum(qryMain.amount)
- (SELECT Sum(X.amount)
FROM qryMain As X
WHERE X.yearfield = qryMain.yearfield - 1)
FROM qryMain
 
M

Marshall Barton

It would depend first on the calculations I needed to do.
If a crosstab does everything you need, then use it, but If
I had to do significant calculations beyond what a crosstab
does, then the non-crosstab would be needed. Only after the
calculations are all accounted for, would I make the choice
according to easy it is to display.
--
Marsh
MVP [MS Access]

Great, what criteria would you use to decide whether or not to use a crosstab
query?

Marshall Barton said:
Ahhh, two row headings. In that case the non-crosstab query
would be more like:

SELECT qryMain.yr, qryMain.ProjectType, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project
AND X.ProjectType = qryMain.ProjectType) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.ProjectType, qryMain.Project

by "item" I meant "record". Each record has a project type, project, and
expense amount. In the crosstab query I used project type and project as my
row headings, the year field as a column heading (to separate the years out),
and the sum of the expense amounts as my value...It looks pretty good...I
want to calculate percentage increases or decreases, but was having that null
problem...I'll try the Nz function

:

I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
--
Marsh
MVP [MS Access]


Ray S. wrote:

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?

:

Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr


Ray S. wrote:
Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...

:

Ray S. wrote:

I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes


This kind of aggregation/summary is best done by using a
subreport based on a query that calculates the differences.

Let's say the name of the query for your main report is
qryMain, then the subreport's record source query could be
something like:

SELECT qryMain.Year, Sum(qryMain.amount)
- (SELECT Sum(X.amount)
FROM qryMain As X
WHERE X.yearfield = qryMain.yearfield - 1)
FROM qryMain
 
R

Ray S.

I think you're right...in the crosstab, I'm not able to use the Nz
function...I found that I can't impose criteria on a "value" crosstab
field...so I'm going back to your non-crosstab to see if I can solve the null
value problem there...Thanks so much for all your help.

Marshall Barton said:
It would depend first on the calculations I needed to do.
If a crosstab does everything you need, then use it, but If
I had to do significant calculations beyond what a crosstab
does, then the non-crosstab would be needed. Only after the
calculations are all accounted for, would I make the choice
according to easy it is to display.
--
Marsh
MVP [MS Access]

Great, what criteria would you use to decide whether or not to use a crosstab
query?

Marshall Barton said:
Ahhh, two row headings. In that case the non-crosstab query
would be more like:

SELECT qryMain.yr, qryMain.ProjectType, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project
AND X.ProjectType = qryMain.ProjectType) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.ProjectType, qryMain.Project


Ray S. wrote:

by "item" I meant "record". Each record has a project type, project, and
expense amount. In the crosstab query I used project type and project as my
row headings, the year field as a column heading (to separate the years out),
and the sum of the expense amounts as my value...It looks pretty good...I
want to calculate percentage increases or decreases, but was having that null
problem...I'll try the Nz function

:

I guess I don't inderstand what "item" you are talking
about. You did mention something about "Project" earlier so
If you want the totals broken down into Project subtotals, I
think you can just make these changes:

SELECT qryMain.yr, qryMain.Project
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1
AND X.Project = qryMain.Project) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr, qryMain.Project

OTOH, a crosstab query makes some sense now. You didn't say
what you were using for the RowHeading, but I assume it's
the "project" field.

To deal with the Null values, use the Nz function around the
year amount field in the calculation. I don't have a grip
on what calculation you are doing in this context so I can't
provide the exact changes you should make.
--
Marsh
MVP [MS Access]


Ray S. wrote:

Thanks Marshall, but I still have the problem that the sum of amounts is
occurring over the entire set of records and not over each item amount for
the two selected years...

How about this approach? I tried making a CrossTab query where the years are
made into columns and the value is the amount...The problem I get there is
that I need to do some math calculations on the values, but in some areas
there were no expense amounts for a particular year...the crosstab query
appropriately puts no value there, but if, for example, I subtract one year's
amount from a valueless one I get a blank...same if I subtract a nonvalue
from a value...blank...how can I make those null values into zero values?

:

Sheesh, I must have been half asleep when I posted that. I
not only forgot the GROUP BY clause but I combined the
calculations so you'd get another error if you fixed the
GROUP BY. This time I actually tried it to make sure it
works on my end:

SELECT qryMain.yr,
Sum(qryMain.SumOfamount) As SumMain,
(SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1) As SumX,
SumMain - SumX As Diff
FROM qryMain
GROUP BY qryMain.yr


Ray S. wrote:
Here's the problem I run into...I used

calling my Main query qryMain
yr being the year field
the amount field is an aggregate sum of individual expense amounts from the
underlying table in the query

SELECT qryMain.yr, Sum(qryMain.SumOfamount)
- (SELECT Sum(X.SumOfamount)
FROM qryMain As X
WHERE X.yr = qryMain.yr - 1)
FROM qryMain

If I create a simple instance of just two entries for expenses for the years
2006 and 2007, the expression seems to work fine; but when there are multiple
entries the expression gives wildly out of whack results...I try to modify it
experimenting but I get errors like that my SumOfamount is not part of an
aggregate function...

:

Ray S. wrote:

I have a report that groups annual business expenses by segment and project
type. The first grouping is on Segment, then within each Segment I group the
ProjectType and order them by the identifying year. The report then returns
the details of individual Projects for each year along with their Expense. In
a group footer I sum the total of Project Expenses for each ProjectType. What
I would like to do is add a footer or line to the report that will show the
difference in Expense amounts for ProjectTypes (not individual projects)
between two comparison years.

Now I get, let's say:

Segment
ProjectType
yr, e.g., 2007
project A $
1
project D $ 2

ProjectSum: $3

yr, e.g., 2006
project M $5

ProjectSum: $5

I'd like to add

(2007 ProjectSum - 2006 ProjectSum) to show the increase or decrese in
annual expenses for ProjectTypes


This kind of aggregation/summary is best done by using a
subreport based on a query that calculates the differences.

Let's say the name of the query for your main report is
qryMain, then the subreport's record source query could be
something like:

SELECT qryMain.Year, Sum(qryMain.amount)
- (SELECT Sum(X.amount)
FROM qryMain As X
WHERE X.yearfield = qryMain.yearfield - 1)
FROM qryMain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top