我的SQL服务器数据库中有一个预订表,其中包含
StartDate(datetime) EndDate(日期时间) TypeID(int)
他们输入ID只会显示预订的类型(在线,个人等)。
我希望能够在提供开始日期和结束日期时创建SQL存储查询,在一列中列出日期以及是否在另一列中预订。为了澄清我想要包括范围内的所有日子,而不仅仅是预订日期。如果没有预订,我想使用'0',因此它可以是0显示可用,或者是类型ID以显示当天的预订类型。
啊,也是为了帮助我在网站上显示我的日历显示,有一个第三和第四列显示是否有预订分别在当天开始,并在当天结束时将是有用的。
对于任何帮助,我都非常感激。
谢谢,
尼克
答案 0 :(得分:1)
因为我不知道你的桌子是什么样的,我想出了这个:
create procedure foo (@startdate date, @enddate date)
as
with bookingtable as (
select CONVERT(date,'20140105',112) as startdate
, CONVERT(date,'20140115',112) as enddate
, 'lala' as type
),
DateRange AS
(
SELECT @startdate DateValue
UNION ALL
SELECT dateadd(dd,1,DateValue)
FROM DateRange
WHERE dateadd(dd,1,DateValue) <= @enddate
)
select d.DateValue, b.type
from DateRange d
left join bookingtable b
on d.DateValue >= b.startdate
and d.DateValue <= b.enddate;
用您的预订表替换预订表,您应该好好去
答案 1 :(得分:0)
在这里从臀部拍摄,但这个答案将帮助您获得所需的位置:
Get a list of dates between two dates using a function
如果您将该答案中的功能与左连接组合到您的表格中,您应该能够得出所需的结果。
像这样(UNTESTED):
SELECT d.thedate, TypeID
FROM dbo.ExplodeDates('20090401','20090531') as d
LEFT JOIN yourtable as tab
ON d.thedate BETWEEN StartDate AND EndDate
当然有一些警告,如多次预订,分组等,还有其他方法可以达到类似的效果。
这是使用CTE的另一种选择。
<强>查询强>
/*Parameters*/
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '01 Jan 2014'
SET @EndDate = '01 Apr 2014'
/*Test Data*/
DECLARE @bookings AS TABLE(StartDate DATETIME, EndDate DATETIME, TypeID INT)
INSERT INTO @bookings VALUES ('01 Feb 2014','02 Feb 2014',1)
INSERT INTO @bookings VALUES ('01 Mar 2014','10 Mar 2014',1)
/*Query Body*/
;WITH date_range (calc_date) AS (
SELECT @StartDate
UNION ALL
SELECT DATEADD(DAY, 1, calc_date)
FROM date_range
WHERE DATEADD(DAY, 1, calc_date) < @EndDate)
SELECT calc_date
, TypeID
, CASE calc_date
WHEN StartDate THEN 1
ELSE 0
END AS IsStartDate
, CASE calc_date
WHEN EndDate THEN 1
ELSE 0
END AS IsEndDate
FROM date_range dr
LEFT JOIN @bookings b
ON dr.calc_date BETWEEN StartDate AND EndDate
OPTION (MAXRECURSION 0);
我不知道您的日历可能包含的天数,因此我添加了OPTION (MAXRECURSION 0);
以获得最大递归限制100,显然这是一个安全措施,因此在关闭它时要小心。 / p>
<强>结果
calc_date TypeID IsStartDate IsEndDate
2014-01-21 00:00:00.000 NULL 0 0
2014-01-22 00:00:00.000 NULL 0 0
2014-01-23 00:00:00.000 NULL 0 0
2014-01-24 00:00:00.000 NULL 0 0
2014-01-25 00:00:00.000 NULL 0 0
2014-01-26 00:00:00.000 NULL 0 0
2014-01-27 00:00:00.000 NULL 0 0
2014-01-28 00:00:00.000 NULL 0 0
2014-01-29 00:00:00.000 NULL 0 0
2014-01-30 00:00:00.000 NULL 0 0
2014-01-31 00:00:00.000 NULL 0 0
2014-02-01 00:00:00.000 1 1 0
2014-02-02 00:00:00.000 1 0 1
2014-02-03 00:00:00.000 NULL 0 0
2014-02-04 00:00:00.000 NULL 0 0
2014-02-05 00:00:00.000 NULL 0 0
2014-02-06 00:00:00.000 NULL 0 0
2014-02-07 00:00:00.000 NULL 0 0
2014-02-08 00:00:00.000 NULL 0 0
2014-02-09 00:00:00.000 NULL 0 0
2014-02-10 00:00:00.000 NULL 0 0
2014-02-11 00:00:00.000 NULL 0 0
2014-02-12 00:00:00.000 NULL 0 0
2014-02-13 00:00:00.000 NULL 0 0
2014-02-14 00:00:00.000 NULL 0 0
2014-02-15 00:00:00.000 NULL 0 0
2014-02-16 00:00:00.000 NULL 0 0
2014-02-17 00:00:00.000 NULL 0 0
2014-02-18 00:00:00.000 NULL 0 0
2014-02-19 00:00:00.000 NULL 0 0
2014-02-20 00:00:00.000 NULL 0 0
2014-02-21 00:00:00.000 NULL 0 0
2014-02-22 00:00:00.000 NULL 0 0
2014-02-23 00:00:00.000 NULL 0 0
2014-02-24 00:00:00.000 NULL 0 0
2014-02-25 00:00:00.000 NULL 0 0
2014-02-26 00:00:00.000 NULL 0 0
2014-02-27 00:00:00.000 NULL 0 0
2014-02-28 00:00:00.000 NULL 0 0
2014-03-01 00:00:00.000 1 1 0
2014-03-02 00:00:00.000 1 0 0
2014-03-03 00:00:00.000 1 0 0
2014-03-04 00:00:00.000 1 0 0
2014-03-05 00:00:00.000 1 0 0
2014-03-06 00:00:00.000 1 0 0
2014-03-07 00:00:00.000 1 0 0
2014-03-08 00:00:00.000 1 0 0
2014-03-09 00:00:00.000 1 0 0
2014-03-10 00:00:00.000 1 0 1
2014-03-11 00:00:00.000 NULL 0 0
2014-03-12 00:00:00.000 NULL 0 0
2014-03-13 00:00:00.000 NULL 0 0
2014-03-14 00:00:00.000 NULL 0 0