Solved

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

Posted on 2011-09-24
4
262 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
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
>. 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

16 Experts available now in Live!

Get 1:1 Help Now