Solved

Selecting, counting and outputting an ordered list

Posted on 2007-03-21
5
157 Views
Last Modified: 2010-03-19
Dear Experts,

I'm trying to create an SQL query to output each unique 'username' along with a few details about their calls from table 'CallBankResponse'. This table contains a record to indicate that a call (from CallBank) has been completed.

The desired output for my query is:

Username  |  Number of calls completed  |  Number of calls allocated
--------------------------------------------------------------------
user1            40                  50
user2            38                  55
user3            35                  59

Here is the structure of my tables:

CallBankResponse
=====================
Call_ID (FK)

CallBank
=====================
Call_ID (PK)
Username


Any help on this one would be greatly appreciated.

Thanks

NIck
0
Comment
Question by:nkewney
  • 3
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
what determines Calls Completed and Calls Allocated?

Can you show us some sample data?

AW
0
 
LVL 1

Author Comment

by:nkewney
Comment Utility
Hi Arthur,

Completed calls are reocrds in CallBankResponse whose Call_ID matches that of CallBank

An example:

CallBank
===============
655      4258      iain      3/16/2007 5:33:04 PM      nick      NULL      4/13/2007 5:33:04 PM      33      13
656      4259      iain      3/16/2007 5:37:51 PM      nick      NULL      4/13/2007 5:37:51 PM      33      13
658      4252      iain      3/16/2007 5:43:08 PM      nick      NULL      4/13/2007 5:43:08 PM      33      13
CallBankResponse
=============
655      494      3/15/2007 1:54:44 PM      3/15/2007 1:57:28 PM      3/15/2007 1:57:39 PM
656      495      3/15/2007 1:57:53 PM      3/15/2007 2:00:17 PM      3/15/2007 2:00:29 PM
657      496      3/15/2007 2:00:52 PM      3/15/2007 2:03:27 PM      3/15/2007 2:03:39 PM
658      361      3/15/2007 2:03:20 PM      3/15/2007 2:05:08 PM      3/15/2007 2:08:01 PM
0
 
LVL 1

Author Comment

by:nkewney
Comment Utility
Apologies, I didn't include the column headers!

CallBank
===============
Call_ID, Outlet_ID, Username, Date
655      4258      iain      3/16/2007 5:33:04 PM      nick      NULL      4/13/2007 5:33:04 PM      33      13
656      4259      iain      3/16/2007 5:37:51 PM      nick      NULL      4/13/2007 5:37:51 PM      33      13
658      4252      iain      3/16/2007 5:43:08 PM      nick      NULL      4/13/2007 5:43:08 PM      33      13
CallBankResponse
=============
Response_ID, Call_ID, Date, Started, Finished
494      655      3/15/2007 1:54:44 PM      3/15/2007 1:57:28 PM      3/15/2007 1:57:39 PM
495      656      3/15/2007 1:57:53 PM      3/15/2007 2:00:17 PM      3/15/2007 2:00:29 PM
496      657      3/15/2007 2:00:52 PM      3/15/2007 2:03:27 PM      3/15/2007 2:03:39 PM
497      658      3/15/2007 2:03:20 PM      3/15/2007 2:05:08 PM      3/15/2007 2:08:01 PM
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
SELECT cb.Username,
    SUM(CASE WHEN cbr.call_id IS NULL THEN 0 ELSE 1 END) AS [Number of Calls Completed],
    COUNT(*) AS [Number of Calls Allocated]
FROM CallBank cb
LEFT OUTER JOIN CallBankResponse cbr ON cbr.call_id = cb.call_id
GROUP BY cg.Username
0
 
LVL 1

Author Comment

by:nkewney
Comment Utility
Perfect Scott, Thanks

Nick
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

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

13 Experts available now in Live!

Get 1:1 Help Now