zoukankan      html  css  js  c++  java
  • ORACLE中的KEEP()使用方法

    转载至:http://blog.csdn.net/aqszhuaihuai/article/details/6434160

    ==============================================

    2种取值:
    DENSE_RANK FIRST 
    DENSE_RANK LAST

    SQL> select * from test;

    ID MC SL
    -------------------- -------------------- -------------------
    1 111 1
    1 222 1
    1 333 2
    1 555 3
    1 666 3
    2 111 1
    2 222 1
    2 333 2
    2 555 2

    9 rows selected

    SQL> 
    SQL> select id,mc,sl,
    2 min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
    3 max(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id)
    4 from test
    5 ;

    ID MC SL MIN(MC)KEEP(DENSE_RANKFIRSTORD MAX(MC)KEEP(DENSE_RANKLASTORDE
    -------------------- -------------------- ------------------- ------------------------------ ------------------------------
    1 111 1 111 666
    1 222 1 111 666
    1 333 2 111 666
    1 555 3 111 666
    1 666 3 111 666
    2 111 1 111 555
    2 222 1 111 555
    2 333 2 111 555
    2 555 2 111 555

    9 rows selected

    SQL>

    不要混淆keep内(first、last)外(min、max或者其他):
    min是可以对应last的
    max是可以对应first的
    SQL> select id,mc,sl,
    2 min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
    3 max(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id),
    4 min(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id),
    5 max(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id)
    6 from test
    7 ;

    ID MC SL MIN(MC)KEEP(DENSE_RANKFIRSTORD MAX(MC)KEEP(DENSE_RANKFIRSTORD MIN(MC)KEEP(DENSE_RANKLASTORDE MAX(MC)KEEP(DENSE_RANKLASTORDE
    -------------------- -------------------- ------------------- ------------------------------ ------------------------------ ------------------------------ ------------------------------
    1 111 1 111 222 555 666
    1 222 1 111 222 555 666
    1 333 2 111 222 555 666
    1 555 3 111 222 555 666
    1 666 3 111 222 555 666

    2 111 1 111 222 333 555
    2 222 1 111 222 333 555
    2 333 2 111 222 333 555
    2 555 2 111 222 333 555

    对于id=1的结果集进行一下解释
    min(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id):id等于1的数量最小的(DENSE_RANK first )为
    1 111 1 
    1 222 1 
    在这个结果中取min(mc) 就是111
    max(mc) keep (DENSE_RANK first ORDER BY sl) over(partition by id)
    max(mc) 就是222;
    min(mc) keep (DENSE_RANK last ORDER BY sl) over(partition by id):id等于1的数量最大的(DENSE_RANK first )为
    1 555 3 
    1 666 3

    在这个结果中取min(mc) 就是555,取max(mc)就是666

    id=2的结果集同理

  • 相关阅读:
    [MFC] MFC 用mciSendString加载WAV资源文件
    [JS] HTML QQ分享界面js代码
    [MFC] MFC 打开HTML资源(用ID版,也可加载到自己的web控件上)
    [ACM_暴力][ACM_几何] ZOJ 1426 Counting Rectangles (水平竖直线段组成的矩形个数,暴力)
    [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
    easyui combobox可编辑的情况下,只能首字母开始过滤的问题选项
    easyui-combobox绑定回车事件注意事项
    easyui-combobox绑定回车事件相关
    jquery-qrcode 生成和读取二维码
    zxing生成二维码和读取二维码
  • 原文地址:https://www.cnblogs.com/wllcs/p/6178192.html
Copyright © 2011-2022 走看看