May 19, 2014 at 7:53 am
I need to create a weekly calendar report showing activities for each sales rep in a particular location. It should have the days of the week (M-F) horizontally across the top, and hours in the day (7am-5pm) vertically along the left hand side. Each day column should show activities scheduled for that rep for that day next to the appropriate time interval. Can anyone help me with an example of code you used to do this? I would really appreciate it. Thanks!
May 19, 2014 at 12:49 pm
We need to know more about the schema you are getting the data from so we can write a query that returns the data you need to produce the report you want. I think I'd do something like this and then pivot the data in the report:
DECLARE @people TABLE (ID INT, name VARCHAR(10));
DECLARE @appointments TABLE
(
ID INT,
PeopleID INT,
AppointmentTime DATETIME
);
/* Calendar table down to the hour as it'll be needed to make sure you have a block for each hour */
DECLARE @calendar TABLE
(
ID INT IDENTITY(1, 1),
theDateStart DATETIME,
theDateEnd DATETIME,
theYear SMALLINT,
theMonth TINYINT,
theDay TINYINT,
theHour TINYINT
);
WITH N AS (
SELECT TOP 1000
ROW_NUMBER() OVER (ORDER BY (
SELECT
NULL
)) AS n
FROM
sys.all_columns AS AC
)
INSERT INTO @calendar
(
theDateStart,
theDateEnd,
theYear,
theMonth,
theDay,
theHour
)
SELECT
DATEADD(HOUR, -N.n,
CONVERT(DATETIME, CONVERT(DATE, GETDATE()))) AS theDateStart,
DATEADD(HOUR, -N.n + 1,
CONVERT(DATETIME, CONVERT(DATE, GETDATE()))) AS theDateEnd,
YEAR(DATEADD(HOUR, -N.n,
CONVERT(DATETIME, CONVERT(DATE, GETDATE())))) AS theYear,
MONTH(DATEADD(HOUR, -N.n,
CONVERT(DATETIME, CONVERT(DATE, GETDATE())))) AS theMonth,
DAY(DATEADD(HOUR, -N.n,
CONVERT(DATETIME, CONVERT(DATE, GETDATE())))) AS theDay,
DATEPART(HOUR,
(DATEADD(HOUR, -N.n,
CONVERT(DATETIME, CONVERT(DATE, GETDATE()))))) AS theHour
FROM
N
INSERT INTO @people
(ID, name)
VALUES
(0, -- ID - int
'Jack' -- name - varchar(10)
),
(1, -- ID - int
'Steve' -- name - varchar(10)
)
INSERT INTO @appointments
(ID, PeopleID, AppointmentTime)
VALUES
(0, -- ID - int
0, -- PeopleID - int
DATEADD(DAY, -7, GETDATE()) -- AppointmentTime - datetime
),
(2, -- ID - int
0, -- PeopleID - int
DATEADD(HOUR, -4, DATEADD(DAY, -7, GETDATE())) -- AppointmentTime - datetime
),
(3, -- ID - int
1, -- PeopleID - int
DATEADD(DAY, -6, GETDATE()) -- AppointmentTime - datetime
),
(4, -- ID - int
1, -- PeopleID - int
DATEADD(HOUR, 2, DATEADD(DAY, -3, GETDATE())) -- AppointmentTime - datetime
)
SELECT
C.ID,
C.theDateStart,
C.theDateEnd,
P.ID,
P.name,
A.ID,
A.PeopleID,
A.AppointmentTime,
DATENAME(WEEKDAY, C.theDateStart) AS theWeekDay
FROM
@calendar AS C
LEFT JOIN @people AS P
JOIN @appointments AS A
ON P.ID = A.PeopleID
ON A.AppointmentTime >= C.theDateStart AND
A.AppointmentTime < C.theDateEnd
WHERE
C.theDateStart >= DATEADD(DAY, -7, CONVERT(DATE, GETDATE()))
ORDER BY
C.theDateStart;
That's the best I can do with the little information you provided.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 19, 2014 at 1:24 pm
It is a SQL Server 2005 database. The activity record contains a startdate (datetime) field as well as an activity type and description. It joins to the employee table by activity.userid = employee.userid. The employee table also contains a location code, so you can select all employees for a particular location. They want the report to have 1 page for each employee within the selected location showing the activities for the upcoming week (date range parms) in the calendar format specified above. Not sure what other information you need. I've never used a matrix in a SSRS report before - is that what I need to use? If so, how do I set it up?
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply