我已经为此工作了一段时间。我想知道如何获得这张桌子:
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