Right a bit of a headache here.
I basically wanted to create a cumulative total of a percentage through a query.
To put that in a more physical way: - NCTYPE Freq cpercent
-
x 2 2/9
-
y 3 5/9
-
z 4 9/9
Where the / signifies a divide to get a percentage. The 9 is just the total of the frequency.
Anyway microsoft provide a way of doing this from their article here: http://support.microsoft.com/kb/304180
However, their formula fails to work in one particular scenario.
The ms formula is: - CumPct: DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& "")/DSum("[ProductSales]","tblSalesByCategory")
-
Which is basically using DSum on the fields to produce the cumulative percentages. The issue is if you have two values that are both the same the [ProductSales]>= will not work correctly.
Here is an example: - Type freq cpercent
-
x 2 100%
-
y 2 100%
This is also incorrect for: - Type freq cpercent
-
x 2 0.5
-
y 1 1
-
z 1 1
The problem is that because it is looking for a value >= to be running the DSUM it just cant do it when there are two of the same values.
Does anyone have a suggestion of how this can be done correctly?
Adding a tiebreaker when the two values are equal solves the problem. If the two values are equal you could use a primary key to decide which to include.
Thanks for fixing up the formula so it works properly: - CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& " AND [PrimaryKeyField]<="&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")
-
5 1939
You would need a tiebreaker when the two values are equal. If the two values are equal you could use a primary key to decide which to include.
The formula would be something like this: - CumPct: DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& " AND [PrimaryKeyField]<"&[PrimaryKeyField])/DSum("[ProductSales]","tblSalesByCategory")
Hmmm... That would exclude the current amount. Try: - CumPct: ( [ProductSalesTotal]+DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& " AND [PrimaryKeyField]<"&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")
Let me know if it works properly.
Steven,
I have just given the code a go.
I'm slightly changing the names of fields etc to reflect what I am doing (I'm not doing product sales that is just the microsoft example.) and so far it has given me better results but not fully correct.
At the moment I am getting the equivalent of the following with your 2nd post code: - uniquekey type freq cpercent
-
1 x 2
-
2 y 2 1
This is all correct apart from the blank first row which should be 0.5, a lot closer than previous methods though.
Edit:
I added a bit more data to confirm what was happening: - NCtype TotalNC CumP1ct
-
x 2
-
y 2 0.666666667
-
z 2 1
Bingo :D
Thanks for the help Steven I've managed to get it right in my initial test.
To get it working right I had to change a couple of parts to your formula so i get: - CumP1c1t: (DSum("[CountOfNCType]","tblParetoEvent","[CountOfNCType]>=" & [TotalNC] & " AND [ID]<=" & [ID]))/DSum("[CountOfNCType]","tblParetoEvent")
In your formula with the microsoft example it would be: - CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& " AND [PrimaryKeyField]<="&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")
Just needed to remove the first addition and sort out the key field criteria. But you definitely sent me on the right path.
Thanks for the help!
ps: If you can make the addition in one of your posts I'll put you down as best answer since you knew it needed a unique tiebreaker in this situation and my posts dont really follow on right.
Adding a tiebreaker when the two values are equal solves the problem. If the two values are equal you could use a primary key to decide which to include.
Thanks for fixing up the formula so it works properly: - CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _
-
"[ProductSales]>=" & [ProductSalesTotal] _
-
& " AND [PrimaryKeyField]<="&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Saiyan Vejita |
last post by:
I have the following SELECT query, the results of which I would delete
from the table they're pulled from:
SELECT A.* FROM SalesOrderPartPrices A
WHERE EXISTS(
SELECT 'Exists' FROM...
|
by: Egor Shipovalov |
last post by:
I'm implementing paging through search results using cursors. Is there a
better way to know total number of rows under a cursor than running a
separate COUNT(*) query? I think PostgreSQL is bound...
|
by: seanseaghan |
last post by:
New to this group, so greetings all!
I am trying to develop query syntax in Access 2000 to accomplish the
following:
Imagine you are in an accounting dept. and you are working on a...
|
by: Dai Hao |
last post by:
Hi all,
I have sql query to search for fields in a rather big view. If I execute the
query in sql server enterprise manager, the results will be displayed in
less than 6 seconds. However, if I...
|
by: Loopsludge |
last post by:
ASP.NET 2.0/ SQL Server 2005/ Login Controls
Does anyone know how to format a SQL query string using asp:loginname
in the WHERE clause?
Alternatively would I need to store the login name upon...
|
by: audiophilechris |
last post by:
Can anyway help me write this query? I've created a DB to help me
manage delivery logistics for items I rent out. I have a table that i
use to record delivery date, delivery time, pickup date,...
|
by: ckpoll2 |
last post by:
Hello,
I'm trying to build a text box in a form that will add hours for a two week period. I have created a query to return the hours in the two week range, but the same name and hour type will...
|
by: Scav |
last post by:
Helpful folks,
We have recently migrated from V7 to V8 of UDB on Windows 2003. We are
now in the process of setting up a rollout deployment to upgrade all
of our user clent PC's from the V7...
|
by: wassimdaccache |
last post by:
Hello
I have a subform base on query. I used a textbox with a control source =
=nz(DCount("ref_ID","checklist","=" & & "and" & "= " & True & " and" & "='" & !! & "' "),0)
Each time we...
|
by: pedalpete |
last post by:
I'm running a few mysql queries, and noticed some really slow behavior.
I ran the explain, and on the first query, only one index is being used when there should be two, and in the second query no...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |