473,499 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cumulative variance

I am pulling my hair out trying to solve this one (presumably because
I am not particularly trained or skilled at access!)

I have a query which summarises the variances that each of my sales
guys have created from their targets. This summarises by week, and
works fine. I then re-use this into a cross-tab which presents a nice
orderly table.

My boss would like to see these variances accumulate, so that we could
make a nice graph in Excel of who is doing well against the baseline
target, and who isn't.

I can't seem to find a way of creating a running total of the
variances that are being created.

e.g

Rep_ID Week No Variance Cumulative
1 1 0.01 0.01
1 2 -0.03 -0.02
1 3 0.01 -0.01
etc etc

I've seen some discussion of Dsum as the tool for this, but I think I
may be missing something......

Any pointers gratefully received!

Matt
UK
Nov 12 '05 #1
4 6475
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You've got a problem. Usually, a running (cumulative) total can be
calculated in a query this way:

SELECT Rep_ID, WeekNo, SUM(RepSales) As SalesSum,

(SELECT SUM(RepSales)
FROM Sales
WHERE Rep_ID = S.Rep_ID AND WeekNo <= S.WeekNO ) As Cumulative

FROM Sales As S
WHERE ... < your criteria > ...
ORDER BY Rep_ID, WeekNo

The subquery in the SELECT clause will calc the running total for the
column RepSales for the query's "current" Rep_ID and WeekNo.

The trouble is you want something like this:

SELECT Rep_ID, WeekNo, VAR(RepSales) As Variance,

(SELECT SUM(VAR(RepSales))
FROM Sales
WHERE Rep_ID = S.Rep_ID AND WeekNo <= S.WeekNO ) As Cumulative

FROM Sales As S
.... etc. ...

Which can't be done, because SQL doesn't allow an aggregate function
to have another aggregate function as part of it's expression (what's
inside the parentheses). So, you'll probably have to run 2 queries to
get the results:

qryVariance:

SELECT Rep_ID, WeekNo, VAR(RepSales) As Variance,
FROM Sales As S
WHERE ... < your criteria > ...
GROUP BY Rep_ID, WeekNo
qryVarianceRunningSum:

SELECT Rep_ID, WeekNo, Variance,

(SELECT SUM(Variance)
FROM qryVariance
WHERE Rep_ID = V.Rep_ID AND WeekNo <= V.WeekNO ) As Cumulative

FROM qryVariance AS V
ORDER BY Rep_ID, WeekNo

You can also substitute the DSum() function for the subquery:

DSum("Variance", "qryVariance",
"Rep_ID=V.Rep_ID AND WeekNo <= V.WeekNO")

Not sure if this will work ('cuz of summing a variance column in
another query). If it doesn't you may want to save the results of
qryVariance to a temp. table and then change qryVarianceRunningSum to
this:

SELECT Rep_ID, WeekNo, Variance, SUM(Variance) As Cumulative
FROM tblVariance_tmp AS V
GROUP BY Rep_ID, WeekNo, Variance

I've found that using a subquery in the SELECT statement of a query
causes Access report's to reject the query, even though the query runs
from query design view! That's when using a domain aggregate function
(e.g.: DSum()) is needed.

- --
MGFoster:::mgf
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBP6qwioechKqOuFEgEQJtRwCfV1e1/ubXicEAWetPXZVBKwiQDLYAn1Ct
gstDlvVS8OF9AAp7i9Y1+l9I
=ZftM
-----END PGP SIGNATURE-----
Matt Larkin wrote:
I am pulling my hair out trying to solve this one (presumably because
I am not particularly trained or skilled at access!)

I have a query which summarises the variances that each of my sales
guys have created from their targets. This summarises by week, and
works fine. I then re-use this into a cross-tab which presents a nice
orderly table.

My boss would like to see these variances accumulate, so that we could
make a nice graph in Excel of who is doing well against the baseline
target, and who isn't.

I can't seem to find a way of creating a running total of the
variances that are being created.

e.g

Rep_ID Week No Variance Cumulative
1 1 0.01 0.01
1 2 -0.03 -0.02
1 3 0.01 -0.01
etc etc

I've seen some discussion of Dsum as the tool for this, but I think I
may be missing something......

Any pointers gratefully received!

Matt
UK


Nov 12 '05 #2
Matt,
if your query was called qryVariances, and had columns called
Rep_ID, Week_Number and Variance then the following query will give a
running sum of the variance for each Rep:

SELECT T1.Rep_ID, T2.Week_Number, Sum(T1.Variance) AS SumOfVariance
FROM qryVariances AS T1 INNER JOIN qryVariances AS T2 ON T1.Rep_ID =
T2.Rep_ID
WHERE (((T2.Week_Number)>=[T1].[Week_Number]))
GROUP BY T1.Rep_ID, T2.Week_Number
ORDER BY T1.Rep_ID, T2.Week_Number;

You could use the results of this query as the input for your crosstab
table. I recommend Celko's "SQL for Smarties" - where I got this
query type.

Good luck,
Martin

go************@mattandsarah.org.uk (Matt Larkin) wrote in message news:<91************************@posting.google.co m>...
I am pulling my hair out trying to solve this one (presumably because
I am not particularly trained or skilled at access!)

I have a query which summarises the variances that each of my sales
guys have created from their targets. This summarises by week, and
works fine. I then re-use this into a cross-tab which presents a nice
orderly table.

My boss would like to see these variances accumulate, so that we could
make a nice graph in Excel of who is doing well against the baseline
target, and who isn't.

I can't seem to find a way of creating a running total of the
variances that are being created.

e.g

Rep_ID Week No Variance Cumulative
1 1 0.01 0.01
1 2 -0.03 -0.02
1 3 0.01 -0.01
etc etc

I've seen some discussion of Dsum as the tool for this, but I think I
may be missing something......

Any pointers gratefully received!

Matt
UK

Nov 12 '05 #3
Ma***************@dsto.defence.gov.au (Iago Gallego) wrote in message news:<a5**************************@posting.google. com>...
Matt,
if your query was called qryVariances, and had columns called
Rep_ID, Week_Number and Variance then the following query will give a
running sum of the variance for each Rep:

SELECT T1.Rep_ID, T2.Week_Number, Sum(T1.Variance) AS SumOfVariance
FROM qryVariances AS T1 INNER JOIN qryVariances AS T2 ON T1.Rep_ID =
T2.Rep_ID
WHERE (((T2.Week_Number)>=[T1].[Week_Number]))
GROUP BY T1.Rep_ID, T2.Week_Number
ORDER BY T1.Rep_ID, T2.Week_Number;

You could use the results of this query as the input for your crosstab
table. I recommend Celko's "SQL for Smarties" - where I got this
query type.

Good luck,
Martin

Not sure if I understood this correctly - the references to T1 and T2
in particular.

The source for the qryVariances is a single table of Rep_ID,
Week_Number etc.

Do I need further reference tables (in the example above, T1, T2)
containing week and rep details?

TIA, much appreciated!

Matt
Nov 12 '05 #4
Matt,
it is confusing, yes. :)

The query I gave works with two copies of the same table/query. To
tell them apart, the FROM clause has the same table twice and gives
each instance an alias (T1 and T2 in this case). If you were to use
the QBE window, it would have the two tables joined on the Rep_Id
column, but no other joins.

If you want to add week and rep details, I would be tempted to add
them afterwards (in a query using the results of this one - but then I
tend to chain my queries. Its may not be efficient, but it fits my
"top-down" programming approach.

Best of luck,
Martin
go************@mattandsarah.org.uk (Matt Larkin) wrote in message news:<91************************@posting.google.co m>...
Ma***************@dsto.defence.gov.au (Iago Gallego) wrote in message news:<a5**************************@posting.google. com>...
Matt,
if your query was called qryVariances, and had columns called
Rep_ID, Week_Number and Variance then the following query will give a
running sum of the variance for each Rep:

SELECT T1.Rep_ID, T2.Week_Number, Sum(T1.Variance) AS SumOfVariance
FROM qryVariances AS T1 INNER JOIN qryVariances AS T2 ON T1.Rep_ID =
T2.Rep_ID
WHERE (((T2.Week_Number)>=[T1].[Week_Number]))
GROUP BY T1.Rep_ID, T2.Week_Number
ORDER BY T1.Rep_ID, T2.Week_Number;

You could use the results of this query as the input for your crosstab
table. I recommend Celko's "SQL for Smarties" - where I got this
query type.

Good luck,
Martin

Not sure if I understood this correctly - the references to T1 and T2
in particular.

The source for the qryVariances is a single table of Rep_ID,
Week_Number etc.

Do I need further reference tables (in the example above, T1, T2)
containing week and rep details?

TIA, much appreciated!

Matt

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2461
by: Victor | last post by:
Hi There, I have a query witch gives me the following result: Period NumberOfItems 1 13 2 2 3 1 4 1 5 1
4
24422
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
2
3190
by: chickenyd | last post by:
Hi i am trying to create a program using WML, WMLSCRIPT, AND JSP. The task is to create a website to be displayed in the WAP Browser. In the website for a company called 'BargainBook' that sells...
0
2453
by: James Hallam | last post by:
I have searched through the news groups and found many threads close to what I want but cannot get any of them to work... I have a table with expenses and invoices, what I want is a bar chart...
3
6875
ChaseCox
by: ChaseCox | last post by:
Hi all, I have a problem that I have been looking at for a couple days now and I can not quite get it to work. I would like to calculate the cumulative percent failure of a certain product in...
1
2457
by: wisemen | last post by:
I have a table with 2 columns, and .I want to run a query that will give me a cumulative sum of the no. of entries that have an <= and on the second column give me a cumulative sum of the no. of...
1
2252
by: cbellew | last post by:
Hi guys, i'm looking to create a report with a table showing totals (running and cumulative) of education sessions attend by the staff at a hospital. I'm trying to get the table to show something...
3
5008
by: eliasam | last post by:
Hi can anyone help me with this? I've got a field in a query to calculate the weight charge: CalcWeightCharge: * Then in another field i would like to calculate the variance from the given...
4
1971
by: JAG | last post by:
The following line of code worked in my .hta prior to installing MS08-045 - Cumulative Security Update for Internet Explorer (953838) (on both XP and W2K): window.top.frames.location = ; After...
0
7178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.