TSQL日历表,从日期开始算起10个工作日
我有一个日历表,用于存储日期行以及该日期是假日还是工作日的指示。TSQL日历表,从日期开始算起10个工作日
如何选择即5个工作日内到从2014-12-22
将来的日期,以便选定的日期将是2014-12-31
Date_Id Date_Date Date_JDE Is_WorkingDay
20141222 2014-12-22 114356 1
20141223 2014-12-23 114357 1
20141224 2014-12-24 114358 1
20141225 2014-12-25 114359 0
20141226 2014-12-26 114360 0
20141227 2014-12-27 114361 0
20141228 2014-12-28 114362 0
20141229 2014-12-29 114363 1
20141230 2014-12-30 114364 1
20141231 2014-12-31 114365 1
可以使用CTE这样的...
;WITH cteWorkingDays AS
(
SELECT Date_Date, ROW_NUMBER() OVER (ORDER BY Date_Date) as 'rowNum'
FROM TableName
WHERE Is_WorkingDay = 1
and Date_Date > '20141222' -- this will be a param I suppose
)
SELECT Date_Date
FROM cteWorkingDays
WHERE rowNum = 5 -- this can be changed to 10 (title value
这是手工输入,但它会足够接近。
编辑:根据评论。
Declare @DateToUse TYPE -- unsure if you're using a string or a date type.
SELECT @DateToUse = Date_Date
FROM cteWorkingDays
WHERE rowNum = 5
感谢您的回答,真的很有帮助。但是,我怎样才能从'cteWorkingDays'中选择值到一个变量? – user667430 2014-09-30 10:09:03
我刚刚添加了一个更新,让我知道如果它没有意义。 – christiandev 2014-09-30 10:12:18
哦,我看到了,我试图使用'SET'来设置变量而不是'SELECT'。谢谢。 – user667430 2014-09-30 10:13:00
...;
WITH DatesCTE AS
(
SELECT Date_Id,
Date_Date,
Date_JDE,
Is_WorkingDay,
ROW_NUMBER() OVER(ORDER BY Date_Date) AS rn
FROM DatesTable
WHERE Is_WorkingDay = 1
AND Date_Date > '2014-12-22'
)
SELECT Date_Date
FROM DatesCTE
WHERE rn = 5
你可以尝试这样的:
with calender as
(select top 5 date_id,date_date,date_jde from calender
where date_date>='2014-12-22' and is_workingday='1)calender
select top 1 * from calender order by date_date desc
前5名的订单是多少?你不想包括第二十二? '=>' – christiandev 2014-09-30 10:05:14
您能否请检查并发布一个sql小提琴来验证这个概念? – Aditya 2014-09-30 10:11:50
与派生表
select * from
(
SELECT Date_Date, ROW_NUMBER() OVER (ORDER BY Date_Date) as 'RowNum'
FROM Table_calendar
WHERE Is_WorkingDay = 1
and CAST(Date_Date as DATE) > '2014-12-22'
)d
where d.RowNum=5
我与** ** 5个工作日内答复因为这是t他质疑价值,但你在标题中有** 10 **。 – christiandev 2014-09-30 10:03:23