Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting, counting and outputting an ordered list

Posted on 2007-03-21
5
Medium Priority
?
215 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
[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
  • 3
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18763002
what determines Calls Completed and Calls Allocated?

Can you show us some sample data?

AW
0
 
LVL 1

Author Comment

by:nkewney
ID: 18763081
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
ID: 18763224
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 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 18767731
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
ID: 18778604
Perfect Scott, Thanks

Nick
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

609 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