Solved

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

Posted on 2011-09-24
4
274 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
[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
  • 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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