数据结构
Road        门号  区域
天山路       25    A
天山路       100   A
天山路       200   B
天山路       365   B
天山路       700   A
天山路       1700  A最后想要得到的结果
Road    min门号  Max门号  区域
天山路    25       100     A
天山路    200      365     B
天山路    700      1700    A由于中间有一段不在区域的数据 ,不知道应该怎么得到这个结果,求助,谢谢。
需要创建临时表去更新最大最小值那种语句就算了。

解决方案 »

  1.   

    select Road,区域,min(门号),Max(门号) 
    from tb
    group by Road,区域
      

  2.   

    Road    min门号  Max门号  区域 
    天山路    25      1700    A 
    天山路    200      365    B 
      

  3.   

    -------------------------------------
    --  Author : liangCK 梁爱兰
    --  Comment: 小梁 爱 兰儿
    --  Date   : 2009-09-30 16:56:12
    -------------------------------------
     
    --> 生成测试数据: @T
    DECLARE @T TABLE (Road varchar(6),门号 int,区域 varchar(1))
    INSERT INTO @T
    SELECT '天山路',25,'A' UNION ALL
    SELECT '天山路',100,'A' UNION ALL
    SELECT '天山路',200,'B' UNION ALL
    SELECT '天山路',365,'B' UNION ALL
    SELECT '天山路',700,'A' UNION ALL
    SELECT '天山路',1700,'A'--SQL查询如下:SELECT Road,MIN(门号) AS 最小门号,MAX(门号) AS 最大门号,区域
    FROM (SELECT *,flag=(SELECT COUNT(*) FROM @T
                         WHERE Road = A.Road AND 门号 < A.门号 AND 区域<>A.区域)
          FROM @T AS A) AS T
    GROUP BY Road,区域,flag;/*
    Road   最小门号        最大门号        区域
    ------ ----------- ----------- ----
    天山路    25          100         A
    天山路    700         1700        A
    天山路    200         365         B(3 行受影响)*/
      

  4.   


    if object_id('tb') is not null drop table tb
     go
    create table tb(Road nvarchar(10),        门号 int, 区域 char(2))
     go
    insert into tb
    select 
    '天山路',      25 ,  'A'  union all select
    '天山路',      100,  'A' union all select
    '天山路',      200,  'B' union all select
    '天山路',      365,  'B' union all select
    '天山路',      700,  'A' union all select
    '天山路',      1700,  'A' alter table tb add id int IDENTITY(1,1)
    select * from tb
    select a.road
      from tb a
    select a.road,a.门号 as min门号,b.门号 as max门号,a.区域
    from
    (select sn=(select count(1)  from tb where id%2<>0 and id<=a.id),a.* from tb a where id%2<>0)a,
    (select sn=(select count(1)  from tb where id%2=0 and id<=a.id),a.* from tb a where id%2=0)b
    where a.sn=b.sn
    /*
    天山路 25 100 A 
    天山路 200 365 B 
    天山路 700 1700 A */
      

  5.   


    if object_id('tb') is not null drop table tb
     go
    create table tb(Road nvarchar(10),        门号 int, 区域 char(2))
     go
    insert into tb
    select 
    '天山路',      25 ,  'A'  union all select
    '天山路',      100,  'A' union all select
    '天山路',      200,  'B' union all select
    '天山路',      365,  'B' union all select
    '天山路',      700,  'A' union all select
    '天山路',      1700,  'A' union all select
    '天山路2',      25 ,  'A'  union all select
    '天山路2',      100,  'A' union all select
    '天山路2',      200,  'B' union all select
    '天山路2',      365,  'B' union all select
    '天山路2',      700,  'A' union all select
    '天山路2',      1700,  'A' alter table tb add id int IDENTITY(1,1)
    select a.road,a.门号 as min门号,b.门号 as max门号,a.区域
    from
    (select sn=(select count(1)  from tb where id%2<>0 and id<=a.id),a.* from tb a where id%2<>0)a,
    (select sn=(select count(1)  from tb where id%2=0 and id<=a.id),a.* from tb a where id%2=0)b
    where a.sn=b.sn
    /*
    天山路 25 100 A 
    天山路 200 365 B 
    天山路 700 1700 A 
    天山路2 25 100 A 
    天山路2 200 365 B 
    天山路2 700 1700 A */加点数据测试!
      

  6.   

    梁哥的最后一个是A,A,B,而楼主的是A,B,A!当然我的也是A,B,A
      

  7.   

    -------------------------------------
    --  Author : liangCK 梁爱兰
    --  Comment: 小梁 爱 兰儿
    --  Date   : 2009-09-30 16:56:12
    -------------------------------------
     
    --> 生成测试数据: @T
    DECLARE @T TABLE (Road varchar(6),门号 int,区域 varchar(1))
    INSERT INTO @T
    SELECT '天山路',25,'A' UNION ALL
    SELECT '天山路',100,'A' UNION ALL
    SELECT '天山路',200,'B' UNION ALL
    SELECT '天山路',365,'B' UNION ALL
    SELECT '天山路',700,'A' UNION ALL
    SELECT '天山路',1700,'A'--SQL查询如下:SELECT Road,MIN(门号) AS 最小门号,MAX(门号) AS 最大门号,区域
    FROM (SELECT *,flag=(SELECT COUNT(*) FROM @T
                         WHERE Road = A.Road AND 门号 < A.门号 AND 区域<>A.区域)
          FROM @T AS A) AS T
    GROUP BY Road,区域,flag
    ORDER BY Road,MIN(门号)  --加个排序也不会?/*
    Road   最小门号        最大门号        区域
    ------ ----------- ----------- ----
    天山路    25          100         A
    天山路    200         365         B
    天山路    700         1700        A(3 行受影响)*/
      

  8.   

    drop table test;
    create table test(Id int identity(1,1), road varchar(15), doornum int, area char(2));insert into test(road, doornum, area)
    select
    '天山路',25,'A' union all select
    '天山路',100,'A' union all select 
    '天山路',200,'B' union all select
    '天山路',365,'B' union all select 
    '天山路',700,'A' union all select 
    '天山路',1700,'A' ;select road, min(doornum), max(doornum), area
    from test
    group by road,(case when Id%2<>0 then Id+1 else Id end), area;
      

  9.   

    谢谢各位 结贴,liangCK
     
    (小梁 爱 兰儿 ❤) 是完全正确的,我自己再看看能不能优化下。
    soft_wsx 和 luoyoumou1202
    的不太对,怪我给的数据不好, 
    再加一行数据
    '天山路',150,'A'
    这样就清楚了。我的错。