2

我有一个名为“详细信息”的表,我在其中存储工作的开始日期和结束日期。我还有一个名为“leaves”的表,它也有休假开始日期和休假结束日期字段。我需要找到用户最近的可用日期没有周末和休假日期。

DECLARE @PackagerLastAssignedDate DATETIME

SELECT @PackagerLastAssignedDate = MAX(EndDate)  FROM detail WHERE userId  = 1 

SELECT lveStartDate,lveEndDate  FROM Leaves WHERE UserId = 1 and lveStartDate > @PackagerLastAssignedDate

提前致谢 Berlin.M

4

1 回答 1

1

试试这个——

DECLARE 
      @DateFrom DATETIME
    , @DateTo DATETIME

SELECT 
      @DateFrom = '20130101'
    , @DateTo = '20130202'

SELECT [Date]
FROM (
    SELECT [Date] = DATEADD(DAY, sv.number, t.DateFrom)
    FROM (
        SELECT 
              DateFrom = @DateFrom
            , diff = DATEDIFF(DAY, @DateFrom, @DateTo)
    ) t
    JOIN [master].dbo.spt_values sv ON sv.number <= diff
    WHERE sv.[type] = 'p'
) t2
WHERE DATENAME(WEEKDAY, [Date]) NOT IN ('Saturday', 'Sunday')
    AND NOT EXISTS (
        SELECT 1 
        FROM dbo.Leaves l
        WHERE l.UserId = 1
            AND t2.[Date] BETWEEN l.lveStartDate AND l.lveEndDate 
    ) 
于 2013-05-28T06:46:57.613 回答