Solved

SQL Query to get call per week, create column based on first day of week

Posted on 2014-02-06
1
875 Views
Last Modified: 2014-06-27
Hi,
I have a Phone Log table consist of CalledBy and CallOn.
I am trying to write a query that show each sales person calls per week.
The data looks like this

Bob Smith     1/7/2014
Bob Smith     1/8/2014
Bob Smith     1/17/2014
Mary Jane      1/8/2014
Mary Jane      1/9/2014

I want the result to look like this

Name           TotalCallsInWeekOf1/7/2014    TotalCallsInWeekOf1/14/2014
Bob Smith                            2                                             1
Mary Jane                             1                                             0

I have a query in progress but it doesn't really do what I needed.
Please help!

Select
A.CalledBy,
A.WeekOf,
sum(A.calls) AS NumOfCallsMade
from
(
    SELECT
    CONVERT(varchar, DATEADD(dd, -(DATEPART(dw, CalledOn)-2), CalledOn), 101) AS WeekOf,
    CalledBy
    ,1 AS calls
    FROM PhoneLog
   
) A
GROUP BY CalledBy, WeekOf
ORDER BY CalledBy, WeekOf
0
Comment
Question by:rskepton
1 Comment
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
For just two weeks specifically, the code below should work ok.

If you really need the actual date in the column name, you will have to use dynamic SQL or a temp table for the results.



DROP TABLE #PhoneLog
CREATE TABLE #PhoneLog (
    CalledBy varchar(30),
    CalledOn datetime
    )
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/5/2014') --should be ignored, before start date
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/6/2014') --should be ignored, before start date
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/7/2014')
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/8/2014')
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/17/2014')
INSERT INTO #PhoneLog VALUES('Bob Smith',     '1/22/2014') --should be ignored, after start date + 2 weeks
INSERT INTO #PhoneLog VALUES('Mary Jane',     '1/8/2014')
INSERT INTO #PhoneLog VALUES('Mary Jane',     '1/9/2014')


DECLARE @start_date datetime
SET @start_date = '20140107'

----------------------------------------------------------------------------------------------------

SELECT
    pl.CalledBy AS Name,
    SUM(CASE WHEN pl.CalledOn >= @start_date AND pl.CalledOn < DATEADD(DAY, 8, @start_date) THEN 1 ELSE 0 END) AS Week1,
    SUM(CASE WHEN pl.CalledOn >= DATEADD(DAY, 7, @start_date) AND pl.CalledOn < DATEADD(DAY, 15, @start_date) THEN 1 ELSE 0 END) AS Week2
FROM #PhoneLog pl --dbo.PhoneLog pl
WHERE
    pl.CalledOn >= @start_date AND
    pl.CalledOn < DATEADD(DAY, 15, @start_date)
GROUP BY
    pl.CalledBy
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now