表A记录:
序号    方向
1       进
2       进
3       进
4       出
5       出
6       出
7       进
8       进
10       进
......想实现这样的结果
1  -  3   进
4  -  6   出
7  -  10  进
请教..........

解决方案 »

  1.   

    看看这个对你有用不?表A记录:1   开门   
    2   开门
    3   关门
    4   开门
    5   开门
    6   关门请教是否能查询出改变量,即只过滤出改变过值的相邻记录。结果应如下:
    1   开门
    3   关门
    4   开门
    6   关门-- 测试数据
    DECLARE @t TABLE(
    id int, value varchar(10))
    INSERT @t SELECT 1, N'开门'
    UNION ALL SELECT 2, N'开门'
    UNION ALL SELECT 3, N'关门'
    UNION ALL SELECT 4, N'开门'
    UNION ALL SELECT 5, N'开门'
    UNION ALL SELECT 6, N'关门'-- 查询
    SELECT *
    FROM @t A
    WHERE value <> ISNULL((
    SELECT TOP 1 value FROM @t
    WHERE id < A.id
    ORDER BY id DESC), A.value + 'a')-- 结果:
    id          value
    ----------- ----------
    1           开门
    3           关门
    4           开门
    6           关门(4 行受影响)
      

  2.   

    我見過子陌兄寫過類似的SQL, 可惜當時沒有收藏
      

  3.   

    declare @ta table(id int, name varchar(2))
    insert @ta
    select 1,       '进'union all
    select 2,       '进'union all
    select 3,       '进'union all
    select 4,       '出'union all
    select 5,       '出'union all
    select 6,       '出'union all
    select 7,       '进'union all
    select 8,       '进'union all
    select 10,       '进'select * from @ta a
    where not exists
    (select 1 from @ta where name=a.name and id<a.id and id=a.id-1) 
    and exists
    (select 1 from @ta where name=a.name and id>a.id and id=a.id+1) (所影响的行数为 9 行)id          name 
    ----------- ---- 
    1           进
    4           出
    7           进(所影响的行数为 3 行)
      

  4.   

    create table ta(id int, name varchar(2))
    insert ta
    select 1,       '进'union all
    select 2,       '进'union all
    select 3,       '进'union all
    select 4,       '出'union all
    select 5,       '出'union all
    select 6,       '出'union all
    select 7,       '进'union all
    select 8,       '进'union all
    select 10,       '进'
    select *,
    记录=(select count(*)from ta a
    where not exists
    (select 1 from ta where name=a.name and id<a.id and id=a.id-1) and id!>b.id),
    max_id=(select max(id)from ta)
    into #--这里生成临时表统计/楼主可以生成视图(简化语句)
     from ta b
    where not exists
    (select 1 from ta where name=b.name and id<b.id and id=b.id-1) 
    and exists
    (select 1 from ta where name=b.name and id>b.id and id=b.id+1)select 显示效果=case when exists(select 1 from # where 记录=a.记录+1 )
    then cast(id as varchar)+' — '+(select cast(id-1 as varchar) from # where 记录=a.记录+1 )
    else cast(id as varchar)+' — '+cast(max_id as varchar) end,
    name
    from # a显示效果                                                             name 
    ---------------------------------------------------------------- ---- 
    1 — 3                                                            进
    4 — 6                                                            出
    7 — 10                                                           进(所影响的行数为 3 行)
      

  5.   

    declare @t table(序号 int,方向 varchar(4))
    insert into @t select 1 ,'进'
    insert into @t select 2 ,'进'
    insert into @t select 3 ,'进'
    insert into @t select 4 ,'出'
    insert into @t select 5 ,'出'
    insert into @t select 6 ,'出'
    insert into @t select 7 ,'进'
    insert into @t select 8 ,'进'
    insert into @t select 10,'进'select
        rtrim(a.序号)+'--'+rtrim(min(b.序号)) as 范围,a.方向
    from
        (select m.* from @t m where not exists(select 1 from @t where 序号=(select max(序号) from @t where 序号<m.序号) and 方向=m.方向)) a,
        (select n.* from @t n where not exists(select 1 from @t where 序号=(select min(序号) from @t where 序号>n.序号) and 方向=n.方向)) b
    where
        a.方向=b.方向 and a.序号<=b.序号
    group by
        a.方向,a.序号/*
    范围                       方向   
    -------------------------- ---- 
    1--3                       进
    4--6                       出
    7--10                      进
    */