[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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

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
GordonPrince
Asked:
GordonPrince
  • 2
2 Solutions
 
mmr159Commented:
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
 
_agx_Commented:
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
 
GordonPrinceAuthor Commented:
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
 
_agx_Commented:
>. 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now