Solved

? MS ACCESS 2010 sample to calculate median quickly?

Posted on 2014-02-15
6
402 Views
Last Modified: 2014-02-20
I have a data base with several payments for each participant.
Each payment has participant number and date-of-payment.

I'd like to easily and QUICKLY calculate the Median number of days between payments for each participant.

I found a method using a sub-query, but, it ran for over 12 hours on only about 30,000 rows.
I'd like to be able to run it on a half-million rows in less than an hour.

help?
0
Comment
Question by:philkryder
  • 2
  • 2
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
What did your query look like?

To start with, you have to get the payment date and the previous payment date,  I generally do that something like:

SELECT P1.ParticipantID, P1.PaymentDate, Max(P2.PaymentDate) as PrevPayment
FROM yourTable as P1
LEFT JOIN yourTable as P2
ON P1.ParticipantID = P2.ParticipantID
AND P1.PaymentDate >P2.PaymentDate
GROUP By P1.ParticipantID, P1.PaymentDate

This should run relatively quickly, but is not editable in the query grid, only in the SQL view.  Once you have that working, you can either save it, or make it a subquery to get the difference in dates between the PaymentDate and PrevPayment.  You might want to make that a temp table or even make the above into a temp table, to speed up succeeding logic.

What logic are you using to compute the median for each participant, once you know the number of days between payments?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
BTW, you might want to check out this article by MatthewsPatrick that discusses Median, Mode, Skewness, and Kurtosis
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
Have you had a chance to review the approach described in my article above?  (Thanks for referring it, Dale!)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:philkryder
Comment Utility
yes - I used the DMEDIAN on an INTERVAL value that I obtained using the code Dale showed for finding the interval between the two dates.

After finding the INTERVAL it would find the DMEDIAN.

I saved intermediate tables after each step.

I was running this on an I3 intel and it pegged 100% of one of the CPU engines for about 12 hours to do my 32k rows.


My typical grouping on Participant will have from 7 to 20 intervals.


Have you run this on similar data quantities?

I'll post the code when I get back to work after the holiday tomorrow.
Phil
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
A sample file would be very helpful
0
 
LVL 1

Author Comment

by:philkryder
Comment Utility
I'd like to close this.
On closer examination, I found the major delay was in calculating the interval between two timestamps.

I subsequently calculated the interval by sorting in order by the key, and the timestamp, then assigning a sequential ID, then doing a self-join on the next row. This seemed much faster than the sub-query to find the interval.

Matthew -
regarding the median -
would you consider adding a couple of global variables such that if the CRITERIA was the same between calls to DMEDIAN, then the DMEDIAN returned would be the same as on the prior call.
Not as elegant,
But, It seems it would avoid creating and running the subquery much of the time  ...
something like:
' =================
Global DMedianSave As Long
Global CriteriaSave As String
...

    If Criteria <> "" Then
   
        If CriteriaSave = Criteria Then
            DMedian = DMedianSave
            GoTo Cleanup
        End If
    End If
    CriteriaSave = Criteria
...
 '        calculate DMEDIAN as before
...
   End If        
    DMedianSave = DMedian
    GoTo Cleanup
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now