http://sqlfiddle.com/#!2/134bad
数据,如果您无法访问该链接:
create table climate (city varchar(10), status char(1), Curdate date);
insert into climate values ('Chennai', 'S', '2013-08-05');
insert into climate values ('Chennai', 'S', '2013-08-06');
insert into climate values ('Chennai', 'S', '2013-08-07');
insert into climate values ('Chennai', 'S', '2013-08-08');
insert into climate values ('Chennai', 'R', '2013-08-09');
insert into climate values ('Chennai', 'R', '2013-08-10');
insert into climate values ('Chennai', 'S', '2013-08-12');
insert into climate values ('Chennai', 'S', '2013-08-13');
insert into climate values ('Chennai', 'R', '2013-08-14');
insert into climate values ('Chennai', 'R', '2013-08-15');
insert into climate values ('Banglore', 'S', '2013-08-05');
insert into climate values ('Banglore', 'S', '2013-08-06');
insert into climate values ('Banglore', 'R', '2013-08-07');
insert into climate values ('Banglore', 'R', '2013-08-08');
insert into climate values ('Banglore', 'R', '2013-08-09');
insert into climate values ('Banglore', 'S', '2013-08-10');
insert into climate values ('Banglore', 'R', '2013-08-12');
insert into climate values ('Banglore', 'R', '2013-08-13');
insert into climate values ('Banglore', 'R', '2013-08-14');
insert into climate values ('Banglore', 'S', '2013-08-15');
该链接包含大致数据。
从表中我们需要检索城市名称和状态('R'/'S')保持相同超过2天的最新最长日期。
即。 R-下雨 S-阳光
当城市连续多雨或晴天超过2天时,我们需要检索城市和最长日期。
例如:来自示例数据,
查询应该检索
City Date
Banglore 2013-08-14
Chennai 2013-08-08
提前感谢您的帮助
答案 0 :(得分:1)
对于SQL Server 2005/2008:
select city, max(dt) max_dt
from (
select city
, dateadd(dd, x, Curdate) dt
, min(case x when 0 then status end) s0
, min(case x when 1 then status end) s1
, min(case x when 2 then status end) s2
from climate c
cross join (select 0 x union all select 1 union all select 2)x
group by city, dateadd(dd, x, Curdate)
) t
where s0 = s1 and s1 = s2
group by city
如果使用SQL Server 2012查询会更加简单。寻找LAG / LEAD功能。
答案 1 :(得分:1)
这与Islands和Gaps问题类似,您也可以使用Common Table Expressions来解决它:
;WITH DateIslandByCityStatus_CTE (City, Status, CurDate, Island) AS
(
SELECT City
, Status
, CurDate
, Island = DATEADD(DAY, -ROW_NUMBER() OVER (PARTITION BY City, Status ORDER BY CurDate), CurDate)
FROM Climate
),
DateIslandWithTwoDaysOfWeather (City, Status, MaxDate) AS
(
SELECT City
, Status
, MAX(CurDate)
FROM DateIslandByCityStatus_CTE
GROUP BY City, Status, Island
HAVING COUNT(*) > 2
)
SELECT City
, Max(MaxDate)
FROM DateIslandWithTwoDaysOfWeather
GROUP BY City
ORDER BY City
另请参阅:"The SQL of Gaps and Islands in Sequences - Dwain Camps"
答案 2 :(得分:0)
如果使用SQL 2005或更高版本:
select city, status, max(endDate) as Date
from
(
select city, status, MIN(curdate) as startDate, MAX(curDate) as endDate
from
(
select city, status, curdate,
DATEADD(dd, - ROW_NUMBER() OVER (PARTITION by city, status ORDER BY city, curdate), curdate)
from climate
group by city, status, curdate
) consecutiveDates(city, status, curdate, grp)
group by city, status, grp
having COUNT(*) > 2
) groupedConsecutiveDates
group by city, status