Solved

Help with MySQL Query

Posted on 2013-01-11
1
385 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 143

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 48
Optimize the query 5 43
issue with DB import 1 20
MS SQL Conditional WHERE clause 3 17
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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…

830 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