私のデータベースには、営業時間のある週に活動できるグループがあります。
グループ A 開始日: 2012 年 10 月 22 日 終了日: 2012 年 10 月 28 日 (開始日は常に週の初め)
グループ A 開始日: 2012 年 10 月 29 日 終了日: 2012 年 4 月 11 日 (常に週の終わり)
グループ A は、この 2 週間で 2 つの異なる営業時間があります。
これで、入力パラメーターの StartDate と EndDate を調べてアクティブなグループを返すストアド プロシージャができました。この 2 つのパラメーターが 1 週間の開始日と終了日の間にある場合、適切な営業時間を取得しています。
しかし、入力パラメーターが約 2 週間 (Begindate: 22-10-2012 および EndDate: 30-10-2012) の場合、2 週間の営業時間を取得します。
declare @Begindate datetime
set @Begindate = '2012-10-22'
declare @Enddate datetime
set @Enddate = '2012-11-02'
SELECT Id, Date, ..., ...
FROM Table1 t1
INNER JOIN Combinations c ON t1.Id=c.Table1Id
INNER JOIN Group g ON c.GroupId=g.Id
WHERE t1.Date<= @Enddate) AND (t1.Date>= @Begindate (gets dates I need)
AND g.BeginDate <=@Enddate and g.Enddate >= @Begindate (Gets active groups)
Some table data:
Table1:
Id Date GroupId
1 2012-10-23 10
1 2012-10-29 10
Combinations: (holds the relation between Table1 and Group)
ID Table1Id GroupId
1 10 1
Group Table:
ID Name StartDate EndDate MondayOpen MondayClose ...
1 Group A 2012-10-22 2012-10-28 08:00 18:00
1 Group A 2012-10-29 2012-11-04 13:00 18:00
このクエリでは、両方の入力パラメーターが 2 週間を超えているため、グループを 2 回取得しています。
グループの開始日と終了日を調べて、(t1 から) 正しい開始日を取得するにはどうすればよいですか?