HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

SQL Server CTE problem

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlproblemcteserver

Problem

This question is in reference to my last question: SQL Server 08: Union over while

I'm writing a CTE to do what was described in that question (find available time slots over a given number of days.

I have a CTE written, but I can't quite grasp how to get my date incrementing and limited. As it stands, it only gets data from the 2011-07-30 (@ArrivalDate). Can anybody explain to me how I can get it properly incrementing, and where I'm going wrong?

Update: I've come up with a better query for getting all the available time slots

(SELECT TS.UKTNN, TS.TimeSlotID, TS.TimeSlotGroupID 
 FROM FittingCentreTimeSlotGroupTimeSlots TS)
EXCEPT
(Select BFD.UKTNN, BFD.TimeSlotID, BFD.TimeSlotGroupID 
 From BookedFittingDates BFD)


And here is the table structure as requested in the comments:

TimeSlotGroup
UKTNN, TimeSlotGroupID, TimeSlotGroupDesc

TimeSlotGroupTimeSlots
UKTNN, TimeSlotID, TimeSlotGroupID, TimeSlotDesc

BookedDates
UKTNN, TimeSlotGroupID, TimeSlotID, Date

Here is what my existing CTE looks like right now

```
Use[DEV_UKTN_DATA]
GO
DECLARE @UKTNN int;
DECLARE @ArrivalDate date;
DECLARE @NumDays int;
DECLARE @LoopDate date;

SET @UKTNN = 7;
SET @ArrivalDate = '2011-07-30';
SET @LoopDate = @ArrivalDate;
SET @NumDays = 2;

WITH GetTimeSlotsOnDays(TimeSlotGroupID, TimeSlotGroupDesc, TimeSlotID, TimeSlotDescription, AdditionalCost, UKTNN, CurrentDate, Distance)
AS
(
SELECT TSG.TimeSlotGroupID,
TSG.TimeSlotGroupDesc,
TS.TimeSlotID,
TS.TimeSlotDescription,
TS.AdditionalCost,
@UKTNN AS UKTNN,
@ArrivalDate as CurrentDate,
0 As Distance
FROM FittingCentreBranchTimeSlotGroups TSG
INNER JOIN FittingCentreTimeSlotGroupTimeSlots TS
ON TSG.TimeSlotGroupID = TS.TimeSlotGroupID AND
TSG.UKTNN = TS.UKTNN
WHERE TSG.UKTNN = @UKTNN AND
TS.UKTNN = @UKTNN AND
TS.TimeSlotGroupID = TSG.TimeSlotGroupID AND
NOT EXISTS(

Solution

I found an answer for this!

1) Create a function that returns a table containing the desired dates

2) Do a cartesian product of the timeslots with the dates

3) Find the exceptions between the booked dates and the cartesian product

Context

StackExchange Database Administrators Q#4316, answer score: 2

Revisions (0)

No revisions yet.