0

我有一个问题要解决。这是怎么回事...

员工休假或休假,并且此条目记录在带有LEAVE_START_DATE和的表中NO_OF_DAYS(这是工作日数)。

主表包含DATEIS_WEEK_DAY(Y/N)、IS_HOLIDAY(Y/N) 和DAY_OF_WEEK

需要生成员工休假的工作日期列表。


桌子leaves

LEAVE_START_DATE, NO_OF_DAYS    
2012-05-17, 3

桌子days

DATE, IS_WEEK_DAY, IS_HOLIDAY, DAY_OF_WEEK
2012-05-17, Y, N, 5 
2012-05-18, Y, N, 6 
2012-05-19, N, N, 7 
2012-05-20, N, N, 1
2012-05-21, Y, Y, 2 
2012-05-22, Y, N, 3

预期成绩

DATE
2012-05-17
2012-05-18
2012-05-22
4

2 回答 2

0

我必须过于简单化-您是否要求选择与table days开始日期和工作日参数匹配的 3 天(已填充)?如果是这样,以下将起作用。此外,您SQL SERVER没有parameterized TOP显示ROWCOUNT.

DECLARE @LeaveStartDate date, @noBusDays int
SET @LeaveStartDate = '2012-05-17'
SET @noBusDays = 3

SELECT TOP (@noBusDays) [DATE] 
FROM [DAYS]
WHERE IS_WEEK_DAY = 'Y' 
AND IS_HOLIDAY = 'N'
AND [DATE] > = @LeaveStartDate
ORDER BY [DATE]
于 2012-06-16T14:44:58.820 回答
0

下面的查询将从 SQL Server 2008 开始工作,否则如果您使用 2005 或以下任何版本,您需要使用 where 子句更改最后一个顶部查询以限制行数

 Declare @leaves table
 (Leave_Start_date datetime,No_Of_days int)

 insert into @leaves 
 values ('2012-05-17',3)

 Declare @days table
 ([Date] datetime,
 IS_WEEK_DAY char(1),
 IS_HOLIDAY char(1),
 Day_OF_THE_WEEK int)

 Insert into @days
 values 
 ('2012-05-17','Y','N',5),
 ('2012-05-18','Y','N',6),
 ('2012-05-19','N','N',7),
 ('2012-05-20','N','N',1),
 ('2012-05-21','Y','N',2)

 ;with cte as
 (
  Select [date] from @days where [Date]>=(select Leave_Start_date from @leaves Limit1)
  and is_WEEK_DAY='Y' and IS_HOLIDAY='N'
 )  
 Select top (select no_of_days from @leaves) [date] from cte
于 2012-06-16T14:50:07.057 回答