Solved

SQL statement to show me 28 days of data from 7 days' input

Posted on 2011-09-24
4
269 Views
Last Modified: 2012-08-13
I've got recurring schedule rows in a table with the following information in a
date/time column and a UserID column:
Mon 11:30 am, Bert
Mon 1:30 pm, Bert
Mon 1:30 pm, Fred
Tue 12:30 pm, Fred
Wed 1:30 pm, Bert -- , etc.
which I've implemented as the first week of May, 2011 (Sunday = vbSunday = 1, etc.).

What SQL statement would allow me to generate a calendar for the next 28 days so
each Mon 11:30 am entry (saved as 5/2/2011 11:30 am) would output
9/26/2011 11:30 am,
10/3/2011 11:30 am,
10/10/2011 11:30 am,
10/17/2011 11:30 am

each Mon 12:30 am entry (saved as 5/2/2011 12:30 am) would output
9/26/2011 12:30 pm,
10/3/2011 12:30 pm, etc.

So I could see for the next 28 days what this group appointment calendar will look like.

The result of the query would be:
9/26/2011 11:30 am, Bert
9/26/2011 01:30 pm, Bert
9/26/2011 01:30 pm, Fred
9/27/2011 12:30 pm, Fred
9/28/2011 01:30 pm, Bert
10/3/2011 11:30 am, Bert
10/3/2011 01:30 pm, Bert
10/3/2011 01:30 pm, Fred
repeating until 28 days into the future.
SELECT a.ApptDayTime, i.InterviewerName
FROM   Availability AS a INNER JOIN
       Interviewer AS i ON a.InterviewerID = i.InterviewerID
WHERE (i.Daily = 0)
  -- AND generate 28 days worth of availability

Open in new window

0
Comment
Question by:GordonPrince
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:mmr159
mmr159 earned 50 total points
ID: 36593453
I'm not sure exactly what you're looking for, but it seems like using DATEADD would get you there.  Not elegant at all, but I think it gets you closer.

http://www.w3schools.com/sql/func_dateadd.asp

SELECT a.ApptDayTime, i.InterviewerName
FROM   Availability AS a INNER JOIN
       Interviewer AS i ON a.InterviewerID = i.InterviewerID
WHERE (i.Daily = 0)
UNION
SELECT DATEADD(day,7,a.ApptDayTime), i.InterviewerName
FROM   Availability AS a INNER JOIN
       Interviewer AS i ON a.InterviewerID = i.InterviewerID
WHERE (i.Daily = 0)
UNION
SELECT DATEADD(day,14,a.ApptDayTime), i.InterviewerName
FROM   Availability AS a INNER JOIN
       Interviewer AS i ON a.InterviewerID = i.InterviewerID
WHERE (i.Daily = 0)
UNION
SELECT DATEADD(day,21,a.ApptDayTime), i.InterviewerName
FROM   Availability AS a INNER JOIN
       Interviewer AS i ON a.InterviewerID = i.InterviewerID
WHERE (i.Daily = 0)

Open in new window

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 450 total points
ID: 36593486
One option is using a CTE.  Note,  the datePart(dw, ...) function is non-deterministic.

DECLARE @dateToday datetime
DECLARE @dateFourWeeksOut datetime

SET @dateToday = convert(datetime, convert(varchar, getDate(), 112), 112)
SET @dateFourWeeksOut = dateAdd(d, 28, @dateToday)

;WITH Calendar
AS
(
  SELECT @dateToday AS CalendarDate
  UNION ALL
  SELECT DATEADD(dd, 1, CalendarDate)
  FROM   Calendar
  WHERE  DATEADD(dd, 1, CalendarDate) <= @dateFourWeeksOut
)
SELECT	i.InterviewerName, 
		convert(datetime, convert(varchar(10), c.CalendarDate, 120) +' '+ convert(varchar, a.ApptDayTime, 114), 121) AS AppointmentDate
FROM	Availability AS a 
			INNER JOIN Interviewer AS i ON a.InterviewerID = i.InterviewerID
			INNER JOIN Calendar c ON datePart(dw, c.CalendarDate) = datePart(dw, a.ApptDayTime)
WHERE	i.Daily = 0
ORDER BY AppointmentDate
OPTION (MAXRECURSION 0);

Open in new window

0
 
LVL 4

Author Closing Comment

by:GordonPrince
ID: 36594906
Excellent, just what I was looking for.

But one unrelated question:
why do the double convert when setting @dateToday

convert(datetime, convert(varchar, getDate(), 112), 112) ?

What have you encountered that has lead you to adopt this (what appears to me to be) needless complexity?

Thanks
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36596843
>. What have you encountered that has lead you to adopt this
> (what appears to me to be) needless complexity?

I've been bitten by several implicit conversion problems. Usually when switching database servers and/or db settings.  But because of that, I always explicit conversion.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Date from a string 4 67
SQL 2005 - Memory Table Column Names 11 73
CREATE DATABASE ENCRYPTION KEY 1 69
View SQL 2005 Job package 16 46
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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