Solved

Help with MySQL Query

Posted on 2013-01-11
1
381 Views
Last Modified: 2013-01-24
Hi,

I have a mysql database which collects phone Call Detail Records (CDR). The fields of the MySQL data / Table is somewhat as follows (i've cut down all whats not necessary):

Phone_Number
Channel_Number
Call_Duration

I need to write a query which would give me a output showing the Average Call Duration of each channel, but the average should be calculated only based on the last 10 calls per channel.

My current query looks as follows:

select Channel_Number, count(0) as cnt, avg(Call_Duration) as ACD from CDR_Table group by Channel_Number

Open in new window


But the above query would give the output based on ALL records available in the table. I just need to only get the average for the last 10 calls per channel.

How can this be achieved?

Thanks!
0
Comment
Question by:shaf81
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 38767450
you need to first extract to have the 10 last calls per channel, this into a temp table...
for this, you need of course to have a field which indicates the date of the call, which I presume you have in the same table ...

anyhow, please check this article on how to retrieve the N "last" calls per "group = channel"
http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/
0

Featured Post

Free Trending Threat Insights Every Day

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

13 Experts available now in Live!

Get 1:1 Help Now