test1(id int,id int2 id int3)
值:
1,1,1,4
2,1,1,6
3,1,1,54,2,2,6
5,2,2,5
6,2,2,8
7,2,2,9
==============================
想读出其中的
1,1,1,4
4,2,2,6读取中间两列相同的数据记录组中的某一行记录!谢谢!

解决方案 »

  1.   


    create table a( id1 int ,id2 int ,id3 int , id4 int);select * from a;insert into a values (1,1,1,4);
    insert into a values(2,1,1,6);
    insert into a values(3,1,1,5);
    insert into a values(4,2,2,6);
    insert into a values(5,2,2,5);
    insert into a values(6,2,2,8);
    insert into a values(7,2,2,9);select id1,id2,id3,id4 
     from a 
    where id1 in ( select min(id1)  
     from a 
                    where id2 = id3 
                 group by id2);
    -- 手头没有oracle数据,使用 sql2000 通过 ,没有特别的东西,oracle也肯定通过
      

  2.   


    --你的列名起的很诡异呀!完全看不懂~~~
    select * 
    from 
    (select row_number() over(partition by col2,col3 order by col1) rn,a.* from test where a.col2=a.col3)
    where rn=1
      

  3.   

    首先感谢楼上的仁兄,但是还不是我要求的SQL,是我描述不够准确!呵呵
    第二列和第三列不一定是相同的数据!谢谢!
    请高手们再帮帮忙!谢谢啦!
      

  4.   


    你这不是自相矛盾啊?如果是指中间两列的组合重复,用1楼的语句.create table a( id1 int ,id2 int ,id3 int , id4 int);
    insert into a values(1,1,1,4);
    insert into a values(2,1,1,6);
    insert into a values(3,1,1,5);
    insert into a values(4,2,2,6);
    insert into a values(5,2,2,5);
    insert into a values(6,2,2,8);
    insert into a values(7,2,2,9);select id1,id2,id3,id4 from a t 
    where id1 in (select min(id1) from a where id2 = t.id2 and id3 = t.id3 group by id2 , id3);drop table a /*
    id1         id2         id3         id4         
    ----------- ----------- ----------- ----------- 
    1           1           1           4
    4           2           2           6
    */
      

  5.   

    SQL> select * from test1;
             1          2          3          4
             1          2          3          4
             2          3          4          5
             2          3          4          6
             6          2          3          7
             1          7          7          1
             4          5          5          8
             1          5          5          9
             2          7          7          0
             9          3          4          9
             0          2          6          7已选择11行。SQL> select id1,id2,id3,id4 from
      2  (select id1,id2,id3,id4,row_number() over(partition by id2,id3 order by id2,id3) as rn from test1)
      3  where rn=2;
             1          2          3          4
             2          3          4          6
             1          5          5          9
             2          7          7          0SQL> select id1,id2,id3,id4 from
      2  (select id1,id2,id3,id4,row_number() over(partition by id2,id3 order by id2,id3) as rn from test1)
      3  where rn=3;
             6          2          3          7
             9          3          4          9SQL> select id1,id2,id3,id4 from
      2  (select id1,id2,id3,id4,row_number() over(partition by id2,id3 order by id2,id3) as rn from test1)
      3  where rn=1;
             1          2          3          4
             0          2          6          7
             2          3          4          5
             4          5          5          8
             1          7          7          1
      

  6.   


    --楼主应该是要这样的吧?
    select * 
    from 
    (select row_number() over(partition by col2,col3 order by col1) rn,a.* from test)
    where rn=1
      

  7.   

    补充上面的一下。
    select *
      from (select row_number() over(partition by id2, id3 order by id1) rn,a.*
      from a)
     where rn = 1 and  id2 = id3rn = 1 这个1就代表你要取其中的哪一行。 取第一行就 rn = 1 第二行就rn = 2