我想将每一条LAST_NO和另外一条FRIST_NO能够联系在一起, 
最后再取MIN(FRIST_NO)和MAX(LAST_NO) 
例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是001-100) 
而第2条的LAST_NO和第3条的FRIST_NO无法联系在一起,则需要单独形成一条记录。 
你这个规则没看懂,另外,是不是描述也错了?

解决方案 »

  1.   

    -->测试数据: @T
    declare @T table (ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into @T
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300'-->通過結果猜了
    select a.CODE,a.FRIST_NO,b.LAST_NO from @T a left join @T b on a.CODE=b.CODE and a.ID=b.ID-1 where a.ID%2=1/*
    CODE FRIST_NO LAST_NO 
    ---- -------- ------- 
    001  001      100
    001  201      300
    */
      

  2.   

    呵呵,谢谢dawugui的关注,
    估计是我真的没有说清楚,呵呵,刚刚接触SQL,是一个十足的菜鸟,
    实际上就是把TEMP表
    ID       CODE             FRIST_NO       LAST_NO 
    --     ---             -----       ----- 
    1         001               001             050 
    2         001               051             100 
    3         001               201             250 
    4         001               251             300 处理成CODE           FRIST_NO         LAST_NO 
    ---         -----           ----- 
    001             001                 100 
    001             201                 300 但是我写的语句:select   code,min(frist_no)   as   frist_no,max(last_no)   as   lastno   
                  from   temp   group   by   code   order   by   frist_no   asc 
    处理出来是
    CODE           FRIST_NO          LAST_NO 
    ---         -----           ----- 
    001             001                 300 
    因为第2条记录的LAST_NO和第3条的FRISTNO是没有连在一起的(第2条记录的LAST_NO是100,而第第3条的FRISTNO是201)
      

  3.   

    -->测试数据: @T
    declare @T table (ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into @T
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300'select t2.*
    from @t t1
    inner join @t t2
    on cast(t1.LAST_NO as int) = cast(t2.FRIST_NO as int) - 1/*
    ID   CODE FRIST_NO LAST_NO 
    ---- ---- -------- ------- 
    2    001  051      100
    4    001  251      300(所影响的行数为 2 行)
    */
      

  4.   

    --这下看明白了,借用小楼兄的数据.
    -->测试数据: @T
    declare @T table (ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into @T
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300'select a.code , b.frist_no , a.last_no 
    from @t a ,@t b
    where a.code = b.code and a.id - 1 = b.id and cast(a.frist_no as int) = cast(b.last_no as int) + 1/*
    code frist_no last_no 
    ---- -------- ------- 
    001  001      100
    001  201      300
    */
      

  5.   

    我估计是这样:我想将每一条LAST_NO和另外一条FRIST_NO能够联系在一起, 
    最后再取MIN(FRIST_NO)和MAX(LAST_NO) 
    例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是050-051,实际取数据001 - 100) 
    例如TEMP表中第3条的LAST_NO能和第4条的FRIST_NO连在一起(也就是250-251,实际取数据201 - 300) 
    而第2条的LAST_NO和第3条的FRIST_NO无法联系在一起(100,201),则需要单独形成一条记录。 
      

  6.   

    --try
    --没有测试,没有优化
    select CODE
           , FRIST_NO
           , LAST_NO=select min(LAST_NO) from (
                     select * from TEMP T
                     where not exists(select 1 from TEMP 
                                       where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)
                       ) Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin 
      

  7.   

    例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是001-100)
    -------------------
    這個表述,唉,無言!例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是050-051)
    這樣不很清晰麽,烏龜你說是不是
      

  8.   

    是的,谢谢大家给的答案,谢谢DAWUGUI,你说对了,这个算法的确有点整人理解万岁
    因为那个TEMP表是我举的例子,所以Limpire 和happyflystone 这两位给的方法虽然可以完成
    但是实际当中,LAST_NO和FRIST_NO是很多而且不固定的,我查了很多SQL方面语句的资料
    依然没有答案,苦恼ING
      

  9.   

    Limpire 兄,谢谢支持哈,我是个刚刚来这里的菜鸟
    呵呵,表述很差,大家谅解哈
      

  10.   

    例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是001-100) 
    ------------------- 
    這個表述,唉,無言! 例如TEMP表中第1条的LAST_NO能和第2条的FRIST_NO连在一起(也就是050-051) 
    這樣不很清晰麽,烏龜你說是不是 
    -------------------------
    反正我最开始五分钟怎么也想不通.是的,谢谢大家给的答案,谢谢DAWUGUI,你说对了,这个算法的确有点整人理解万岁 
    因为那个TEMP表是我举的例子,所以Limpire   和happyflystone   这两位给的方法虽然可以完成 
    但是实际当中,LAST_NO和FRIST_NO是很多而且不固定的,我查了很多SQL方面语句的资料 
    依然没有答案,苦恼ING----------------------------------------
    现在我这个算法行不?
      

  11.   

    多谢dawugui ,我试了一下,没有问题,我明天再用其他数据测试一下,多谢了哈~~!!
      

  12.   

    dawugui都被恐惧了N次了,happyflystone下诅咒的能力。。那是相当的不错
      

  13.   

    感觉happyflystone的方法也不错,稍做一下调整就可以了
    declare @T table (ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into @T
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300'select t2.code,t1.frist_no,t2.last_no
    from @t t1
    inner join @t t2
    on cast(t1.LAST_NO as int) = cast(t2.FRIST_NO as int) - 1
    正在学习mssql,受益颇多,谢谢大家。
      

  14.   

    倒回一百年,我满清执政,逮了你丫的,给你丫上满清十大酷刑,整不死你?   
    ===>   
    倒回一龟年,我绿龟执政,逮了你,给你清龟十八煮吃,撑不死你?--------
    我想龟应该还是喜欢SM多一点。嘿嘿```
      

  15.   

    create table TEMP(ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into TEMP
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300'select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from (
                     select * from TEMP T
                     where not exists(select 1 from TEMP 
                                       where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)
                       ) Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin drop table TEMP/*
    CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      300(2 row(s) affected)
    */
      

  16.   

    我的理解龟兄的一样,,,
    create table TEMP(ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into TEMP
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300' union all
    select 5,'001','301','350' union all
    select 6,'001','401','500' select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from (
                     select * from TEMP T
                     where not exists(select 1 from TEMP 
                                       where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)
                       ) Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin drop table TEMP/*
    CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      350
    001  401      500(3 row(s) affected)
    */
      

  17.   

    create table TEMP(ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into TEMP
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300' union all
    select 5,'001','301','350' union all
    select 6,'001','401','500' GO
    --SQL2005
    WITH Tmax as 
    (
        select * from TEMP T
        where not exists(select 1 from TEMP 
    where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)

    select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin --SQL2000
    select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from (
                     select * from TEMP T
                     where not exists(select 1 from TEMP 
                                       where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)
                       ) Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin 
    drop table TEMP/*
    CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      350
    001  401      500(3 row(s) affected)CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      350
    001  401      500(3 row(s) affected)
    */
      

  18.   

    pt1314917 兄,GUI兄,你们给的方法我试过了
    但是如果记录条输超过2条,就不行了
    而且如果CODE字段发生改变统计结果也发生错误
    大家能麻烦再帮帮忙吗?谢谢熊兄给的方法,非常感谢,明天我尝试一下你给的方法。明天我就结贴了,谢谢大家的帮助,在此向大家道谢了~~!
      

  19.   

    lllyyymmm 兄同样热心~~!
    学海无涯啊。
      

  20.   

    连续记录超过2条,CODE有多个,都没有问题,,,
    create table TEMP(ID tinyint,CODE varchar(3),FRIST_NO varchar(3),LAST_NO varchar(3))
    insert into TEMP
    select 1,'001','001','050' union all
    select 2,'001','051','100' union all
    select 3,'001','201','250' union all
    select 4,'001','251','300' union all
    select 5,'001','301','350' union all
    select 6,'002','401','500' union all
    select 7,'002','501','550' union all
    select 8,'002','601','750' union all
    select 9,'003','801','900' union all
    select 10,'003','901','922' union all
    select 11,'003','923','950' 
    GO
    --SQL2005
    WITH Tmax as 
    (
        select * from TEMP T
        where not exists(select 1 from TEMP 
    where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)

    select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin --SQL2000
    select CODE
           , FRIST_NO
           , LAST_NO=(select min(LAST_NO) from (
                     select * from TEMP T
                     where not exists(select 1 from TEMP 
                                       where CODE=T.CODE and cast(T.LAST_NO as int)=cast(FRIST_NO as int)-1)
                       ) Tmax where CODE=Tmin.CODE and LAST_NO>=Tmin.LAST_NO)
    from (
           select * from TEMP T
           where not exists(select 1 from TEMP where CODE=T.CODE and cast(LAST_NO as int)=cast(T.FRIST_NO as int)-1)
          ) Tmin 
    drop table TEMP/*
    CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      350
    002  401      550
    002  601      750
    003  801      950(5 row(s) affected)CODE FRIST_NO LAST_NO
    ---- -------- -------
    001  001      100
    001  201      350
    002  401      550
    002  601      750
    003  801      950(5 row(s) affected)*/
      

  21.   

    dawugui,dobear_0922,Hobart,happyflystone,Limpire
    谢谢大家的帮助和支持,第一次发贴,开始的时候分给少了,
    因为大熊兄的答案完全能够完成查询,所以给了30,请大家多多理解了
    今后一定把这个分给补上,再这里再次谢谢各位的帮助了~~!