zoukankan      html  css  js  c++  java
  • oracle 中select into ,group by ,partition,max 等的相关使用【总结】

    今天在处理oracle数据库时遇到如下问题,经过一番挣扎,终于搞定:

    想要的功能也就是比如文件的版本,有第1,2,3,4版本,我現在要找出所有文件的最大(小)的版本號: 

    Specid         specno     specvision

    Random11      a            1

    Random21      a            2

    Random31      a            3

    Random12      b            1

    Random22      b            2 

    結果我想要的是:

    Specid         specno       specvision

    Random31      a            3

    Random22      b            2 

    如果單純的求specno ,specvision  也很是容易的

    SELECT   max(specvsion) as vision,specno

    FROM   table_name

    group by  specno

    求的:   specno                      vision

                 a                                3

                 b                                2

    但是我更想要的是specid ???

    第一次想到用select  Into 解决,并没有成功:

    SQL的使用是:  select * into table_Name    from table_name1   where   .......................         table_name 并不存在

    oracle的使用是:  create table  (或者是Global temporary table) table_Name  as

                            select * from  ............ where .........

     第二次如下验证是成功的:

    select *
    from (
          select t_tcm_form_zp.*,row_number()over(partition by specno order by specver desc)rn
          from t_tcm_form_zp
    )
    where rn=1

    做一点解释:row_number() over ( partition by col1 order by col2 desc/asc ) ) as 别名
                  表示根据col1分组,在分组内部根据 col2(升/降)排序
                  而这个“别名”的值就表示每组内部排序后的顺序编号(组内连续的唯一的)

     

    另外:发现一个比较好的SQL语句网站,比较基础的!

          http://www.w3school.com.cn/sql/

  • 相关阅读:
    20180929 北京大学 人工智能实践:Tensorflow笔记02
    20180929 北京大学 人工智能实践:Tensorflow笔记01
    YOLOv3学习笔记
    编辑器上传漏洞
    IIS解析漏洞利用
    数据库备份及审查元素进行webshell上传
    burp suite 进行webshell上传
    BUGKU CFT初学之WEB
    CTFbugku--菜鸟初学
    理解PHP中的会话控制
  • 原文地址:https://www.cnblogs.com/alexzp/p/2288814.html
Copyright © 2011-2022 走看看