0

我需要生成如下记录:

我有一个tbl_Graph带有列的表DBName, Size, JobName, StartDate, Enddate

---------------------------------------------------------------------------
DBName  |  Size    |   JobName  |       StartTime        |  EndTime       |
--------------------------------------------------------------------------|
DB1     |  13.8    |    JOB1    |       4/7/2013 20:44   |  4/7/2013 21:05|
DB2     |  10.5    |    JOB2    |       4/7/2013 18:20   |  4/7/2013 19:09|
DB3     |  09.10   |    JOB4    |       4/7/2013 3:04    |  4/7/2013 3:23 |
DB4     |  13.8    |    JOB5    |       4/7/2013 11:49   |  4/7/2013 11:54|
DB5     |  10.5    |    JOB3    |       4/7/2013 16:04   |  4/7/2013 16:30|
DB6     |  09.10   |    JOB6    |       4/7/2013 14:02   |  4/7/2013 14:04|
DB7     |  13.8    |    JOB7    |       4/7/2013 20:05   |  4/7/2013 21:21|
DB8     |  10.5    |    JOB8    |       4/7/2013 19:03   |  4/7/2013 19:17|
DB9     |  09.10   |    JOB9    |       4/7/2013 0:17    |  4/7/2013 11:07|
DB10    |  13.8    |    JOB11   |       4/7/2013 1:05    |  4/7/2013 1:40 |
---------------------------------------------------------------------------

如果我将 from 和 to date 指定为“4/7/2013”​​,那么它将根据该日期获取所有匹配的记录,并且它应该根据每行的开始和结束时间每分钟生成记录。

我需要这样的结果集

---------------------------------
Date             |     JobName  |       
---------------------------------
4/7/2013 20:44   |      JOB1    |       
4/7/2013 20:45   |      JOB1    |   
4/7/2013 20:46   |      JOB1    |  
4/7/2013 20:47   |      JOB1    |   
4/7/2013 20:48   |      JOB1    |   
4/7/2013 20:49   |      JOB1    |   
4/7/2013 20:50   |      JOB1    |   
4/7/2013 20:51   |      JOB1    |  
4/7/2013 20:52   |      JOB1    |   
4/7/2013 20:53   |      JOB1    |   
4/7/2013 20:54   |      JOB1    |   
4/7/2013 20:55   |      JOB1    |   
4/7/2013 20:56   |      JOB1    |   
4/7/2013 20:57   |      JOB1    |   
4/7/2013 20:58   |      JOB1    |   
4/7/2013 20:59   |      JOB1    |   
4/7/2013 21:01   |      JOB1    |   
4/7/2013 21:02   |      JOB1    |  
4/7/2013 21:03   |      JOB1    |  
4/7/2013 21:04   |      JOB1    |  
4/7/2013 21:05   |      JOB1    |  
---------------------------------

如何在没有任何性能问题的情况下实现这一目标?

我试过的解决方案:

我通过使用游标和 while 循环实现了这一点,但它会导致性能问题,例如需要 60 多秒才能获取记录。

4

1 回答 1

2

试试这个——

询问:

DECLARE @temp TABLE
(
        JobName VARCHAR(10)
      , StartTime DATETIME
      , EndTime DATETIME
)

INSERT INTO @temp (JobName, StartTime, EndTime)
VALUES 
('JOB1',  '20130704 20:44',  '20130704 21:05'),
('JOB2',  '20130704 18:20',  '20130704 19:09'),
('JOB4',  '20130704 3:04 ',  '20130704 3:23 '),
('JOB5',  '20130704 11:49',  '20130704 11:54'),
('JOB3',  '20130704 16:04',  '20130704 16:30'),
('JOB6',  '20130704 14:02',  '20130704 14:04'),
('JOB7',  '20130704 20:05',  '20130704 21:21'),
('JOB8',  '20130704 19:03',  '20130704 19:17'),
('JOB9',  '20130704 0:17 ',  '20130704 11:07'),
('JOB11', '20130704 1:05 ',  '20130704 1:40 ')

SELECT 
      JobName
    , DateCol = DATEADD(MINUTE, sv.number, StartTime)
FROM (
    SELECT 
          StartTime
        , diff = DATEDIFF(MINUTE, StartTime, EndTime)
        , JobName
    FROM @temp
) t
JOIN [master].dbo.spt_values sv ON sv.number <= diff
WHERE sv.[type] = 'p'

输出:

JobName    DateCol
---------- -----------------------
JOB1       2013-07-04 20:44:00.000
JOB1       2013-07-04 20:45:00.000
JOB1       2013-07-04 20:46:00.000
JOB1       2013-07-04 20:47:00.000
JOB1       2013-07-04 20:48:00.000
JOB1       2013-07-04 20:49:00.000
JOB1       2013-07-04 20:50:00.000
JOB1       2013-07-04 20:51:00.000
JOB1       2013-07-04 20:52:00.000
JOB1       2013-07-04 20:53:00.000
JOB1       2013-07-04 20:54:00.000
JOB1       2013-07-04 20:55:00.000
JOB1       2013-07-04 20:56:00.000
JOB1       2013-07-04 20:57:00.000
JOB1       2013-07-04 20:58:00.000
JOB1       2013-07-04 20:59:00.000
JOB1       2013-07-04 21:00:00.000
JOB1       2013-07-04 21:01:00.000
JOB1       2013-07-04 21:02:00.000
JOB1       2013-07-04 21:03:00.000
JOB1       2013-07-04 21:04:00.000
JOB1       2013-07-04 21:05:00.000
...
于 2013-05-29T07:38:32.830 回答