|
I have CallLog table that has receiveddate column and closeddate column. I need to figure out total # of calls that were open each month. Some calls are open for multiple months. I am not a fulltime SQL Programmer so I am having trouble comming up with the logic to retrieve this info. Can anyone guide me in the right direction?
(comments are locked)
|
|
Please bear in mind this is really not optimised at all, and assumes you have a table called tblCalls with columns StartMonth and EndMonth. Treat it as a guide only...
(comments are locked)
|
|
I am using a temporary table, #CallLog, in my example with the columns you specified. I am using a recursive CTE to generate a 'months' table with the beginning of the month and the beginning of the next month. I then do a CROSS JOIN between the months and the calls (joining every call to every month - to look for open calls in that month). The conditions for an open call are 1) the call was received before or during the given month and 2) the call was closed after the given month. If those conditions are wrong the query would need to be modified accordingly. The variables @startMonth, @endMonth would probably become parameters to a stored procedure. The results show the month (as the first day of the month) and the number of open calls at the end of that month. I think the fact he tagged it as SQL Server 2000 means that this solution won't work as it uses a CTE.
Oct 20 '09 at 06:06 AM
Melvyn Harbour 1 ♦♦
(comments are locked)
|
|
This is written using SQL Server 2005/2008 CTEs for the example, but they are not necessary for the solution.
. To make the SELECT statement work, you just need 2 things: 1) Create a calendar table like this:
2) Use a unique identifier in your CallLog table. I used CallLogId in the example. As long as you have those 2 things, then you can use this simple statement to get what you need:
. Example Results:
(comments are locked)
|
|
How you do convert this to use it minute by minute?
(comments are locked)
|


How are you defining an open call? Is it that the reeiveddate value falls within a month - meaning the call was opened that month and the closeddate is immaterial? Or is it that the receiveddate falls within a month and the call has an empty closeddate? Assuming that it's counting calls that have a NULL closeddate, and the call had a receiveddate in an earlier month, would that call be counted once per month, or only in the month that it was opened? If you could post your table structure and some sample data that would be helpful.