SQL Server 2008,临时表,游标

Posted

技术标签:

【中文标题】SQL Server 2008,临时表,游标【英文标题】:SQL Server 2008, temp tables, cursor 【发布时间】:2011-08-17 21:16:32 【问题描述】:

我已经为此工作了一段时间。我想知道如何才能得到这张桌子:

id       open_dt             ops_hrs
1        10/31/2011          7:00AM - 5:30PM
2        11/1/2011           7:00AM - 5:00PM
3        11/2/2011           7:00AM - 5:00PM
4        11/3/2011           7:00AM - 5:00PM
5        11/6/2011           7:00AM - 7:00PM
6        11/8/2011           7:00AM - 5:00PM

看起来像这个表:

max_date          min_date     ops_hrs
10/31/2011        10/31/2011   7:00AM - 5:30PM
11/1/2011         11/3/2011    7:00AM - 5:00PM
11/6/2011         11/6/2011    7:00AM - 7:00PM
11/8/2011         11/8/2011    7:00AM - 5:00PM

我尝试使用游标,但没有必要。此外,它必须被分组。只要连续几天休息,就会发生新的分组。任何帮助将不胜感激。

此查询将生成上述示例数据

;
WITH pdog (id, open_dt,ops_hrs) AS
(
SELECT 1, CAST('10/31/2011' AS datetime),  '7:00AM - 5:30PM'
UNION ALL SELECT 2, CAST('11/1/2011' AS datetime),'7:00AM - 5:00PM'
UNION ALL SELECT 3, CAST('11/2/2011' AS datetime),'7:00AM - 5:00PM'
UNION ALL SELECT 4, CAST('11/3/2011' AS datetime),'7:00AM - 5:00PM'
UNION ALL SELECT 5, CAST('11/6/2011' AS datetime),'7:00AM - 7:00PM'
UNION ALL SELECT 6, CAST('11/8/2011' AS datetime),'7:00AM - 5:00PM'
)
SELECT * FROM pdog 

【问题讨论】:

如果有一行11/4/2011 7:00AM - 6:00PM会怎样? 这将是另一个组,最小和最大日期为 11/04/2011 所以分组是根据 ops_hrs 的值以及前一天是否是前一天。 yes -- 连续几天被组合在一起 找到了两篇对我在SQL Server中制作游标和临时表很有帮助的文章Click here to read Temporary in SQL server Table on infoA2z.comClick here to read Cursor in SQL server on infoA2z.com 【参考方案1】:
;WITH    CTE
          AS ( SELECT   * ,
                        DATEDIFF(DAY, 0, open_dt) -  ROW_NUMBER() OVER 
                              ( PARTITION BY ops_hrs ORDER BY open_dt ) AS Grp
               FROM     @x
             )
    SELECT  
            MIN(open_dt) AS min_date ,
            MAX(open_dt) AS max_date ,
            ops_hrs
    FROM    CTE
    GROUP BY ops_hrs ,
            Grp
    ORDER BY min_date

【讨论】:

+1 非常简洁整洁,但乍一看我很难理解 DATEDIFF - ROW_NUMBER() 逻辑。【参考方案2】:

肯定比@Martin 的解决方案稍微复杂一些,但我至少应该得到一点,因为他使用了我的@x 表——所以他的解决方案看起来更整洁。 :-)

DECLARE @x TABLE(id INT IDENTITY(1,1), open_dt DATE, ops_hrs VARCHAR(32));

INSERT @x(open_dt, ops_hrs) VALUES
    ('2011-10-31', '7:00AM - 5:30PM'),
    ('2011-11-01', '7:00AM - 5:00PM'),
    ('2011-11-02', '7:00AM - 5:00PM'),
    ('2011-11-03', '7:00AM - 5:00PM'),
    ('2011-11-06', '7:00AM - 7:00PM'),
    ('2011-11-08', '7:00AM - 5:00PM');

;WITH d AS
(
    SELECT open_dt, ops_hrs, max_date = COALESCE((SELECT MAX(open_dt)
        FROM @x AS b WHERE b.open_dt > a.open_dt 
        AND NOT EXISTS (SELECT 1 FROM @x AS c
          WHERE c.open_dt >= a.open_dt 
          AND   c.open_dt <  b.open_dt 
          AND   c.ops_hrs <> b.ops_hrs)), open_dt)
    FROM @x AS a
)
SELECT 
    min_date = MIN(open_dt),
    max_date, 
    ops_hrs
FROM d
    GROUP BY max_date, ops_hrs
    ORDER BY min_date;

【讨论】:

除了变量名之外,该脚本看起来很像我显示器上的内容

以上是关于SQL Server 2008,临时表,游标的主要内容,如果未能解决你的问题,请参考以下文章

在 Sql Server 中,如何将游标中的值放入临时表中?

SQL SERVER2008里面sql语句删除临时表失败

如何从临时表中检索字段名称 (SQL Server 2008)

SQL server2008 临时表tempdb如何自动释放存储空间

sql server 2008r2的tempdb很大怎么办

SQL Server 2008:将 VIEW 与其他 VIEW 联接:预先计算而不求助于临时表