2013-08-15 3 views
1

http://sqlfiddle.com/#!2/134bad비트 복잡한 SQL 쿼리

데이터에 도움이 필요하십니까 :

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-Raining S-Sunny

도시가 Rainy 나 Sunny에 연속으로 2 일 이상 계속있을 때 도시와 최대 날짜를 검색해야합니다.

예 :

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 서버 2005/2008을 위해 여러분의 도움이

+0

사용중인 SQL Server 버전은 무엇입니까? – GolfWolf

+0

SQL Server 2008을 사용 중입니다. – user2664051

답변

1

사전에

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

감사를 검색한다 SQL Server 2012 쿼리를 사용하면 훨씬 간단 해집니다. LAG/LEAD 기능을 찾으십시오.

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 

참조 : "The SQL of Gaps and Islands in Sequences - Dwain Camps"

+0

정말 고맙습니다.이 기능은 저에게 효과적이었습니다 ... – user2664051

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