Solved

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

Posted on 2014-02-06
1
879 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
ID: 39840562
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

929 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

12 Experts available now in Live!

Get 1:1 Help Now