Mark's profileElectron CloudPhotosBlogLists Tools Help

Blog


    June 11

    Geeks: Oracle database query discussion

    前两天有位同学突然出了一个有关oracle database查询的问题,感觉很有趣:
     
    问题嘛,就是一个DB的表,有
    项目1     项目2     项目3     项目4
    A          1          XXX1     AB
    A          1          XXX2     AB
    。。。
    这样的项目,希望能够做出一个查询产生
    项目1     项目2    项目3     项目4
    A          1         XXX1     AB
                          XXX2          
     
    很有意思的问题,我先说个大概想法,抛砖引玉一把。需要先按照分组列进行group by然后选出每一个组里面的第一行的rowid,然后把这个结果集和原来的结果集进行join。
    先做个oracle 测试:
     
    drop table tab_test;
    create table tab_test
    (
    GRP VARCHAR2(10),
    COL1 VARCHAR2(10),
    COL2 VARCHAR2(10)
    );
    insert into tab_test values('G1','R1', 'Q1');
    insert into tab_test values('G1','R2', 'Q2');
    insert into tab_test values('G1','R3', 'Q3');
    insert into tab_test values('G2','R4', 'Q4');
    insert into tab_test values('G2','R5', 'Q5');
    insert into tab_test values('G3','R6', 'Q6');
    commit;
    SQL> select * from tab_test;
    GRP        COL1       COL2
    ---------- ---------- ----------
    G1         R1         Q1
    G1         R2         Q2
    G1         R3         Q3
    G2         R4         Q4
    G2         R5         Q5
    G3         R6         Q6
    我们有G1,G2,G3三个组,选择每一组的第一行
     
    select GRP,MIN(COL1) MCOL1,MIN(COL2) MCOL2
    from tab_test
    group by GRP
    order by GRP;
    GRP        MCOL1      MCOL2
    ---------- ---------- ----------
    G1         R1         Q1
    G2         R4         Q4
    G3         R6         Q6
    我们用这个结果集和原来的表内容进行join,有如下SQL语句:
    select tb.GRP,COL1,COL2
    from tab_test ta,
    (
      select GRP,MIN(COL1) MCOL1,MIN(COL2) MCOL2
      from tab_test
      group by GRP
      order by GRP
    ) tb
    where ta.GRP = tb.GRP(+) and COL1 = MCOL1(+) and COL2 = MCOL2(+)
    order by ta.GRP,ta.COL1,ta.COL2;
     
    GRP        COL1       COL2
    ---------- ---------- ----------
    G1            R1           Q1
                   R2           Q2
                   R3           Q3
    G2            R4           Q4
                   R5           Q5
    G3            R6            Q6
    我想这个就是想要的结果集吧。这个方案只是个初步设想,如果在生产系统上使用,还需要进一步优化。
    如果结果集比较大,可以按照如下方案优化:
     
    select tb.GRP, ta.COL1, ta.COL2
    from tab_test ta,
    (
    select GRP,min(rowid) KEEP (DENSE_RANK FIRST ORDER BY COL1 ASC,COL2 ASC) row_id
    from tab_test group by GRP
    ) tb
    where ta.rowid = tb.row_id (+)
    order by ta.GRP,ta.COL1,ta.COL2;
     
     

    Comments (3)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Picture of Anonymous
    Cristina wrote:
    阅。不懂。路过。飘走~~
    July 3
    Mark Jiwrote:
    Sorry for the copyright stuff the question was stolen from you. But the answer is worth sharing with other developers in torture.:-) Have a test on the system and I am more than pleased to support this case.
    June 12
    wrote:
    这是我的问题,知识产权部在哪里?我要投诉!
    不过答案谢谢啦!
    June 11

    Trackbacks

    The trackback URL for this entry is:
    http://electron-cloud.spaces.live.com/blog/cns!A19B848180A533FA!197.trak
    Weblogs that reference this entry
    • None