473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rolling tally through query not correct using dsum

374 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. NCTYPE   Freq   cpercent
  2. x        2      2/9
  3. y        3      5/9
  4. 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:
Expand|Select|Wrap|Line Numbers
  1. CumPct: DSum("[ProductSales]","tblSalesByCategory", _
  2. "[ProductSales]>=" & [ProductSalesTotal] _
  3. & "")/DSum("[ProductSales]","tblSalesByCategory") 
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. Type    freq    cpercent
  2. x       2       100%
  3. y       2       100%

This is also incorrect for:

Expand|Select|Wrap|Line Numbers
  1. Type   freq  cpercent
  2. x      2     0.5
  3. y      1     1
  4. 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?
Aug 4 '10 #1

✓ answered by Steven Kogan

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:

Expand|Select|Wrap|Line Numbers
  1. CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _    
  2. "[ProductSales]>=" & [ProductSalesTotal] _    
  3. & " AND [PrimaryKeyField]<="&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")  
  4.  

5 1939
Steven Kogan
107 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. CumPct: DSum("[ProductSales]","tblSalesByCategory", _ 
  2. "[ProductSales]>=" & [ProductSalesTotal] _ 
  3. & " AND [PrimaryKeyField]<"&[PrimaryKeyField])/DSum("[ProductSales]","tblSalesByCategory")  
Aug 4 '10 #2
Steven Kogan
107 Expert 100+
Hmmm... That would exclude the current amount. Try:

Expand|Select|Wrap|Line Numbers
  1. CumPct: ( [ProductSalesTotal]+DSum("[ProductSales]","tblSalesByCategory", _  
  2. "[ProductSales]>=" & [ProductSalesTotal] _  
  3. & " AND [PrimaryKeyField]<"&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")
Let me know if it works properly.
Aug 4 '10 #3
munkee
374 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. uniquekey  type  freq  cpercent
  2. 1          x     2     
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. NCtype    TotalNC        CumP1ct
  2. x         2       
  3. y            2         0.666666667
  4. z         2           1
Aug 5 '10 #4
munkee
374 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. CumP1c1t: (DSum("[CountOfNCType]","tblParetoEvent","[CountOfNCType]>=" & [TotalNC] & " AND [ID]<=" & [ID]))/DSum("[CountOfNCType]","tblParetoEvent")
In your formula with the microsoft example it would be:


Expand|Select|Wrap|Line Numbers
  1. CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _   
  2. "[ProductSales]>=" & [ProductSalesTotal] _   
  3. & " 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.
Aug 5 '10 #5
Steven Kogan
107 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. CumPct: ( DSum("[ProductSales]","tblSalesByCategory", _    
  2. "[ProductSales]>=" & [ProductSalesTotal] _    
  3. & " AND [PrimaryKeyField]<="&[PrimaryKeyField]))/DSum("[ProductSales]","tblSalesByCategory")  
  4.  
Aug 5 '10 #6

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

Similar topics

6
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...
7
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...
0
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...
1
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...
4
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...
2
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,...
2
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...
3
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...
2
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...
3
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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
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...
0
tracyyun
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...
0
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,...

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.