Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-24
4
Medium Priority
?
286 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 200 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 1800 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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