求连续数据

来源:互联网 发布:上司不允许辞职 知乎 编辑:程序博客网 时间:2024/05/18 01:10

(select * from @t c where not exists(select 1 from @t where 名称=c.名称 and 年份=c.年份 and 月份=c.月份-1)) a,
求最小值
(select * from @t d where not exists(select 1 from @t where 名称=d.名称 and 年份=d.年份 and 月份=d.月份+1)) b
求不连续数据的起始值


按照名称查出同一年中其中查出连续6个月有数据的名称。



select 名称,年份,cast(min(月份) as varchar(2))+'-'+cast(max(月份+5) as varchar(2)) 月份
from @t t
where (
      select count(distinct(a.月份))
        from @t a
       where a.年份=t.年份  and a.名称=t.名称 and a.月份 between t.月份 and t.月份+5
) >5
group by 名称,年份
order by 名称,年份