ADezii,
i have been reading your solution to this percentile problem and believe you can help me.
I have a similar problem with creating a percentile in access 2007.
My table is called "TBL_DATA" and the field with all the values i would like to gain a percentile for is called "DAYS"
i want to develop a query which shows the following columns
- Min Days
- Max days
- Avg Days
- Percentile 95th
- Percentile 75th
- Percentile 50th
- Percentile 25th
i have created the first 3 columns in design view however am having problems creating the percentile calculations.
I would like access to look at all values under "DAYS" and bring back the percentile requested.
I am happyusing either a VBA script or a SQL statement as long as it gets the right answer.
Hope you can help, Thanking you in advance.
19 11119 @SachinPatel - Copy and Paste the following 'Public' Function to a Standard Code Module:
- Public Function fCalculatePercentiles(sngPercentile As Single)
-
'Make sure to 1st set a Reference to the
-
'Microsoft Excel XX.X Object Library
-
Dim sngNumbers() As Single
-
Dim intNumberOfRecords As Integer
-
Dim objExcel As Excel.Application
-
Dim intCounter As Integer
-
Dim MyDB As DAO.Database
-
Dim rstPercentile As DAO.Recordset
-
-
Set MyDB = CurrentDb()
-
Set rstPercentile = MyDB.OpenRecordset("TBL_DATA", dbOpenSnapshot)
-
rstPercentile.MoveLast: rstPercentile.MoveFirst 'accurate Record Count
-
-
intNumberOfRecords = rstPercentile.RecordCount
-
-
'Redimension the Array to proper values
-
ReDim sngNumbers(1 To intNumberOfRecords)
-
-
Set objExcel = CreateObject("Excel.Application")
-
-
'Populate the Array with values from the Recordset
-
For intCounter = 1 To intNumberOfRecords
-
With rstPercentile
-
sngNumbers(intCounter) = ![Days]
-
.MoveNext
-
End With
-
Next
-
-
'Pass the Array to Excel's Percentile Function and Round the result to 2 Decimal Places
-
fCalculatePercentiles = Round(objExcel.Application.Percentile(sngNumbers(), sngPercentile), 2)
-
-
'Don't forget to clean up!!!
-
rstPercentile.Close
-
Set rstPercentile = Nothing
-
objExcel.Quit
-
Set objExcel = Nothing
-
End Function
- Execute the following SQL Statement given your Table and Field Names:
- SELECT Min(TBL_DATA.Days) AS MinOfDays, Max(TBL_DATA.Days) AS MaxOfDays, Avg(TBL_DATA.Days) AS AvgOfDays,
-
fCalculatePercentiles(0.95) AS [95th Percentile],
-
fCalculatePercentiles(0.75) AS [75th Percentile],
-
fCalculatePercentiles(0.5) AS [50th Percentile],
-
fCalculatePercentiles(0.25) AS [25th Percentile]
-
FROM TBL_DATA
-
GROUP BY fCalculatePercentiles(0.25);
- Note that you are passing the actual Percentile Value to the fCalculatePercentiles() Function within the SQL Statement.
- Good Luck and Good Night!
ADezil,
sorry to bother you buti have doneas you stated above however when i try run the query i am getting the error message as follows:
Undefined Function 'fCalculatePercentiles' in expression
is there a certain way i shouldbe saving the VBA code to ensure the query can refer to it?
@SachinPatel
As previously stated in Post #30, Item 1, it must be defined as ' Public' in a Standard Code Module.
Sorry mate - i believe i am defining the item as public, however as this is new to me maybe i am doing it wrong- can you possible tell me the correct way to define the function as public in a standard code module?
@SachinPatel
I'll do better than that. I'll be in work all day, but when I get home this evening I'll send you an Attachment indicating how, exactly, this is done. Be advised that this has only been tested on an insignificant amount of data, and may be incredibly slow on a large number of Records since you are using 3 Aggregate Functions, as well as several Calls to the same Public Function with different Parameters, within the Query. You may also experience some difficulty since you are essentially making the same Function Calls by passing different values (Percentiles). All the good stuff I'll leave to you - see you later. OK, was able to get it done here.
thanks mate your a life saver - i will log on later (5 hours or so)
thanks again your a star!
@SachinPatel
Just in case you didn't notice the previous Post, I was able to create the Attachment in work. Good Luck.
NeoPa 32,556
Expert Mod 16PB
I have split this away from Percentile calculation in Access as it is a different question.
I know you're very new so we'll let it slide this time, but posting your question in an existing thread is hijacking, and is not allowed on this site.
I don't, for a moment, want to stop you asking questions, simply guiding you do do it the proper way in future.
Welcome to Bytes!
NB. Post #6 has the attachment ADezii promised you. Don't overlook it simply because it's not the latest post ;)
hey ADezii,
just seeing if you had chance to send over how to make a vba function public and refer toit on the sql you created without getting an error message.
Thanks
Hey Adezii
thanks for that - wheni open the attachment i get error message saying macros action failed - should this happen?
also i looked at VB and still unable to confirmi if i am correctly creating a public function my SQL query can reference.
NeoPa - apologise for the misunderstanding of how these forums work!
just noticed when i create my module i comes under the heading unrelated objects while yours comes under the modules heading - this could be the reason?
if so how can i do it so it looks like yours and comes under modules heading?
sorry - its not that i have foudn the way to get the heading as modules and still not referring to module!
NeoPa 32,556
Expert Mod 16PB
From Access : - Alt-F11 to switch to the VB Editor.
- Ctrl-R for the Project Explorer pane.
- Right-Click in there and select Insert / Module.
- Enter your code in here and save it.
@NeoPa
Hello NeoPa, just wanted to let you know that I managed to send the Attachment to the OP via his Private E-Mail Address and all seems to have worked out well. Thanks.
NeoPa 32,556
Expert Mod 16PB
8-)
Thanks for posting to clarify :)
Alternatively, percentile could be calculated using SQL facilities only.
The following example gives 90th percentile of [Value] field in [tbl] -
SELECT Max([Value]) AS Percentile
-
FROM
-
(SELECT TOP 90 PERCENT [Value]
-
FROM [tbl]
-
ORDER BY [Value] ASC);
-
@FishVal
Hell FishVal, just thought that you would find the following interesting in that there is a slight discrepancy between the 2 Methods, we'll simply call it FishVal vs Excel! -
Percentiles 95th 75th 50th 25th
-
FishVal 100 88 67 21
-
Excel 101.15 88.25 67 22.5
-
P.S. - Results compiled with Sample Data listed below (Excel results rounded to 2 Decimal Places): - 21
-
7
-
34
-
2
-
67
-
33
-
100
-
89
-
67
-
23
-
88
-
100
-
77
-
16
-
44
-
77
-
11
-
77
-
89
-
123
:)
I know that the results are different.
This happens because dataset is by definition a set of discrete values, so SQL rounds percentile down to existing value while Excel seems to make interpolation. Wikipedia article says both methods are in use.
Regards,
Fish.
PS: FYI, Excel could operate with up to 8191 values while SQL is limited by RDBMS specifications for max records.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: tomvr |
last post by:
Hello
I have noticed some 'weird' memory usage in a vb.net windows app
The situation is as follows
I have an app (heavy on images) with 2 forms
(actually there are more forms and on starting...
|
by: Ian Taite |
last post by:
Hello,
I'm exploring why one of my C# .NET apps has "high" memory usage, and
whether I can reduce the memory usage.
I have an app that wakes up and processes text files into a database...
|
by: jd142 |
last post by:
Normally when people ask about getting the Nth percentile from a group,
the response is to write a couple of VBA modules. But it seems to me
there's a simpler way that just uses 2 queries. You...
|
by: Dave Veeneman |
last post by:
I'm looking from an algorithm that will do a statistics calculation for me--
it's the first time 'Numerical Recipies in C' has failed me! A normal
distribution table tells me the probability (by...
|
by: Joe |
last post by:
Has anyone written a percentile rank algorithm that they can share or
know of a good resource that defines the algorithm. I'm starting a
project that involves percentile ranks, logistic and linear...
|
by: rdemyan via AccessMonster.com |
last post by:
My app contains utility meter usage. One of the things we have to deal with
is when a usage is clearly incorrect. Perhaps someone wrote the meter
reading down incorrectly or made a factor of 10...
|
by: Sirisha |
last post by:
I am using the following code to get the CPU usage
PerformanceCounter myCounter;
myCounter = new PerformanceCounter();
myCounter.CategoryName = "Processor";
myCounter.CounterName = "%...
|
by: riaane |
last post by:
Hey clever people, Excel has the =PERCENTILE function. This calulates a percentile value from an array of values. Is there any equivalent in Access?
|
by: artemetis |
last post by:
Hello!
It's me again.
tblEmp
-->empUid - self explanatory
-->empPerf - ranking from 0 - 100
I'm interested in creating a query that displays the top x% (eg, 20%) of employee performance,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |