将缺少日期范围添加到结果记录集

时间:2010-08-17 11:59:39

标签: sql-server-2005

我有一张包含公司工作时间的表格。我们定义日期的范围和范围的小时数。 不在规定范围内的工作小时数为9.5小时。

我希望将未定义的范围值9.5添加到结果记录集中。 我应该如何进行查询以获得此结果?

表定义和添加的记录:

IF OBJECT_ID ('dbo.tbl_WorkingHours') IS NOT NULL
    DROP TABLE dbo.tbl_WorkingHours
GO

CREATE TABLE dbo.tbl_WorkingHours
    (
    Startdate    DATETIME NOT NULL,
    EndDate      DATETIME NOT NULL,
    HoursDefined FLOAT NULL,
    Description  VARCHAR (255) NULL,
    PRIMARY KEY (Startdate,EndDate)
    )

INSERT INTO dbo.tbl_WorkingHours
    (Startdate,EndDate,HoursDefined,Description)
VALUES 
    ('3/4/2010','3/29/2010',7,'')
INSERT INTO dbo.tbl_WorkingHours
    (Startdate,EndDate,HoursDefined,Description)
VALUES 
    ('5/4/2010','5/29/2010',8,'')

选择结果*:

Startdate  |  EndDate | HoursDefined  | Description
 ----------------------------------------------------
3/4/2010    3/29/2010   7   
5/4/2010    5/29/2010   8

我想要的记录集:

Startdate  |  EndDate | HoursDefined  | Description
----------------------------------------------------
1/1/1900    3/3/2010    9.5
3/4/2010    3/29/2010   7   
3/30/2010   5/3/2010    9.5
5/4/2010    5/29/2010   8
5/30/2010   1/1/2050    9.5

2 个答案:

答案 0 :(得分:0)

以下假设不可能有重叠的范围或两个定义的连续但保持为单独行的范围。

WITH wh AS
(

SELECT     Startdate, EndDate, HoursDefined, Description,
       ROW_NUMBER() over (order by startdate) as rn
FROM         tbl_WorkingHours
) 
SELECT Startdate, EndDate, HoursDefined, Description 
FROM wh
UNION ALL
SELECT ISNULL(dateadd(day,1,w1.EndDate),'19000101'),
       ISNULL(dateadd(day,-1,w2.StartDate),'20500101') , 9.5 AS HoursDefined,''
FROM wh w1 FULL OUTER JOIN wh w2
ON w2.rn = w1.rn+1
ORDER BY Startdate  

答案 1 :(得分:0)

尝试这样的事情:

DECLARE @tbl_WorkingHours table (Startdate    DATETIME NOT NULL
                                ,EndDate      DATETIME NOT NULL
                                ,HoursDefined FLOAT NULL
                                ,Description  VARCHAR (255) NULL
                                ,PRIMARY KEY (Startdate,EndDate)
                                )

INSERT INTO @tbl_WorkingHours (Startdate,EndDate,HoursDefined,Description) VALUES     ('3/4/2010','3/29/2010',7,'')
INSERT INTO @tbl_WorkingHours (Startdate,EndDate,HoursDefined,Description) VALUES     ('5/4/2010','5/29/2010',8,'')


;WITH OrderedRows AS
(   SELECT
        Startdate,EndDate,HoursDefined,Description, ROW_NUMBER() OVER (ORDER BY Startdate,EndDate) AS RowNumber
        FROM @tbl_WorkingHours
)
SELECT --before rows
    CONVERT(datetime,'1/1/1900') AS Startdate,ISNULL(MIN(Startdate),CONVERT(datetime,'1/2/2050'))-1 AS EndDate,9.5 AS HoursDefined, CONVERT(VARCHAR (255),'') AS Description
    FROM @tbl_WorkingHours
UNION ALL
SELECT --actual rows
    Startdate,EndDate,HoursDefined,Description
    FROM @tbl_WorkingHours
UNION ALL
SELECT --between rows
    a.EndDate+1,b.Startdate-1,9.5,''
    FROM OrderedRows            a
        INNER JOIN OrderedRows  b ON a.RowNumber=b.RowNumber-1
UNION ALL
SELECT --after rows
    ISNULL(MAX(EndDate),CONVERT(datetime,'1/1/2050')) AS Startdate,CONVERT(datetime,'1/2/2050')-1 AS EndDate,9.5 AS HoursDefined, CONVERT(VARCHAR (255),'') AS Description
    FROM @tbl_WorkingHours
    ORDER BY Startdate,EndDate

输出:

Startdate               EndDate                 HoursDefined           Description
----------------------- ----------------------- ---------------------- -----------
1900-01-01 00:00:00.000 2010-03-03 00:00:00.000 9.5                    
2010-03-04 00:00:00.000 2010-03-29 00:00:00.000 7                      
2010-03-30 00:00:00.000 2010-05-03 00:00:00.000 9.5                    
2010-05-04 00:00:00.000 2010-05-29 00:00:00.000 8                      
2010-05-29 00:00:00.000 2050-01-01 00:00:00.000 9.5                    

(5 row(s) affected)