?
Solved

Help with MySQL Query

Posted on 2013-01-11
1
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 600 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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