1

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 天时,我们需要从表中检索城市名称和最近的最大日期。

IE。R-Raining S-Sunny

我们需要检索城市以及该城市连续下雨或晴天超过 2 天时的最大日期。

例如:从示例数据中,

查询应该检索

City                  Date
Banglore           2013-08-14
Chennai            2013-08-08

在此先感谢您的帮助

4

3 回答 3

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 函数。

于 2013-08-15T13:54:46.613 回答
1

这类似于孤岛和差距问题,您也可以使用公用表表达式来解决它:

;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

另见:“序列中的间隙和岛屿的 SQL - Dwain Camps”

于 2013-08-15T14:07:21.647 回答
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
于 2013-08-15T14:14:15.997 回答