有如下两个表“table1”、“table2”表中ID列是自增+1的主键,wee列是int型       
       
表“table1”       
ID wee      
1 45      
2 21      
3 21      
4 54      
5 78      
6 45      
7 24      
8 96      
9 23      
10 56      
11 76      
12 34      
13 95      
14 52      
15 56      
16 12      
17 51      
18 32      
19 78      
20 59      
       
表“table2”只有6行数据       
ID wee fbit     
1 6 1     
2 6      
3 4 1     
4 7 1     
5 4      
6 2      
       
对上面两个表的查询要求如下:       
       
用table2的行数6来对table1分组,table1中相邻6行为一组(以相邻ID号分),步进值是3行。Table1在查询时被分为如下5组:       
      
一 二 三 四 五   
ID ID ID ID ID   
1 4 7 10 13   
2 5 8 11 14   
3 6 9 12 15   
4 7 10 13 16   
5 8 11 14 17   
6 9 12 15 18          
用table2上的三行(列fbit=1)wee值的大小及所在行的顺序作为条件,来查询table1中满足条件的组。       
也就是table2的wee值为三个(6,4,7),位置是第一行、第三行、第四行。一行的wee值6大于三行的4并小于四行的7;三行的4小于一行和四行;四行7大于一行和三行。用和前面相同的行位置和大小条件,来查table1的五个分组。满足相同条件的是如下三组:       
      
ID wee | ID wee | ID wee
1 45 | 7 24 | 10 56
2 21 | 8 96 | 11 76
3 21 | 9 23 | 12 34
4 54 | 10 56 | 13 95
5 78 | 11 76 | 14 52
6 45 | 12 34 | 15 56
       
     
  
想得到两个表的查询结果如下(查询出table1中出满足条件的每一组的第一个ID值):       
       
ID wee      
1 45      
7 24      
10 56      我被这个问题困了几天了,我做出来的查询是用了几个视图,再加上两个查询的语句,可以得到正确的结果,但没有实际用途,table1有2百万行,查询时很慢,又要不停修改参数,我的代码就不贴出来影响大家了。

解决方案 »

  1.   

    ID ID ID ID ID   
    1 4 7 10 13   
    2 5 8 11 14   
    3 6 9 12 15   
    4 7 10 13 16   
    5 8 11 14 17   
    6 9 12 15 18   以第一行为例1 4 7 10 13 ,为第一个记录加3就是后面的ID数,直接取值不就成了
      

  2.   

    可能写的不清楚,再简单补充一下。我想用table2作为样板,对 table1进行查询比对,找出与table2中wee值排列及相对大小相同的组。最后想得到查询的结果是:
    ID wee   
    1 45   
    7 24   
    10 56 查询要求是:用整个table2所有行为一个长度,以此长度在table1上一段一段比较,每次移动三行。
    找出table1中与table2中三行wee值相对一样,比对组中ID位置一样的组。
      

  3.   

    有几个疑问,请楼主说明一下:表table2中fbit=1的行数和位置是确定的么,也就是fbit=1只有三行么?一直都在1、3、4行么?
    表table2的行数和移动的步长是确定的么?
      

  4.   

    表table2中fbit=1的行数和位置是变化的,可能有5行fbit=1,也可能是2行fbit=1,table2的总行数是固定的6行,移动的步长也是固定的3。
      

  5.   


    --开始拼条件SQL
    declare @where varchar(1000)
    set @where = ''declare @cn int, @i int, @id int, @wee int, @id1 int, @wee1 int
    select rowid=identity(int,1,1),id,wee into #t from table2 where fbit = 1
    set @cn = @@rowcount
    set @i = 1
    while @i < @cn
    begin
    select @id=id, @wee=wee from #t where rowid = @i
    declare cur cursor for 
    select id, wee from #t where rowid > @i
    open cur
    fetch next from cur into @id1,@wee1
    while @@fetch_status = 0
    begin
    if @wee = @wee1 
    set @where = @where + ' and t'+ltrim(@id)+'.wee=t'+ltrim(@id1)+'.wee'
    else if @wee > @wee1
    set @where = @where + ' and t'+ltrim(@id)+'.wee>t'+ltrim(@id1)+'.wee'
    else
    set @where = @where + ' and t'+ltrim(@id)+'.wee<t'+ltrim(@id1)+'.wee' fetch next from cur into @id1,@wee1
    end
    close cur
    deallocate cur set @i = @i + 1
    end
    set @where = 'where '+stuff(@where,1,5,'')+' and t1.id%3 = 1'--开始执行
    declare @sql varchar(8000)
    set @sql = '
    select t1.* 
    from table1 t1
    join table1 t2 on t1.id = t2.id - 1
    join table1 t3 on t2.id = t3.id - 1
    join table1 t4 on t3.id = t4.id - 1
    join table1 t5 on t4.id = t5.id - 1
    join table1 t6 on t5.id = t6.id - 1 ' + @where + ' order by t1.id'
    exec(@sql)drop table #t/*
    ID          wee
    ----------- -----------
    1           45
    7           24
    10          56(3 行受影响)
    */
    2百万行的话,估计在10秒以内
      

  6.   

    请coleling有时间再看一下如何改,我试了上面的代码。SQL Server 2008中提示是:
    消息 8108,级别 16,状态 1,第 6 行
    无法使用 SELECT INTO 语句将标识列添加到表 '#t',该表的列 'id' 已继承了标识属性。
    谢谢!
      

  7.   



    select rowid=identity(int,1,1),id,wee into #t from table2 where fbit = 1
    改成
    select rowid=row_number() over(order by getdate()),id,wee into #t from table2 where fbit = 1
      

  8.   

    第 6 行 没问题了,新提示是:
    消息 207,级别 16,状态 1,第 11 行
    列名 'rowid' 无效。
    消息 207,级别 16,状态 1,第 13 行
    列名 'rowid' 无效。
      

  9.   

    逻辑太复杂了,看的头大,没办法写SQL,不过也写不出来 支持下给力。
      

  10.   

    是ACCESS?
    Dim ee As Recordset
    Dim rst As Recordset
    Dim dd1
    CurrentDb.Execute "DROP TABLE NEWTT"
    CurrentDb.Execute "DROP TABLE NEWTTA"
    dd1 = "SELECT top 1 id as id1,(select id from ttq1 where id=a.id+3) as id2,(select id from ttq1 where id=a.id+6) as id3,(select id from ttq1 where id=a.id+9) as id4, (select id from ttq1 where id=a.id+12) as id5 into newtt from  ttq1 a where id<=6 "
    CurrentDb.Execute dd1
    Set ee = CurrentDb.OpenRecordset("select id from ttq2 where fbit='1'")
    qq = ""
    Do While Not ee.EOF
    qq = qq & "select id" & ee(0) & " as newid from newtt union "
    ee.MoveNext
    Loop
    qq = "select * INTO NEWTTA from (SELECT * FROM (" & Left(qq, Len(qq) - 7) & " ) a " & " left join ttq1 b on a.newid=b.id)"
    'qq = "SELECT * FROM (" & Left(qq, Len(qq) - 7) & " ) a " & " left join ttq1 b on a.newid=b.id"
    CurrentDb.Execute qq
      

  11.   

    在SQLSERVER中要用SP来实现,思路是一样的
      

  12.   

    很谢谢coleling
    有coleling出现,一定能有惊喜。
    结贴了,很想能多给coleling点分,因我等级有限,只能给100分。谢谢