?
Solved

Selecting, counting and outputting an ordered list

Posted on 2007-03-21
5
Medium Priority
?
212 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 69

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

743 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