zoukankan      html  css  js  c++  java
  • Oracle 单列去重 显示单行所有列数据

    问题:test_table 表中有 a,b,c 三个字段,求根据字段a 去除重复数据,得到去重后的整行数据

    根据mysql的经验尝试以下方法均失败

    1、使用 distinct 关键字 (oracle查询数据中,不允许非 distinct 标注字段 )

    select count(distinct a),a,b,c from test_table;



    2、使用 group by (oracle查询数据中不允许非分组字段)

    select a,b,c from test_table group by a;



    解决方案:使用row_number() over( partition by) 方法分组排序

        with temp as (
             select a,b,c from test_table
        )
         
        select a,b,c from (
               select a,b,c,row_number() over (partition by a order by rownum) row_no from temp
        ) where row_no = 1

    ps:   with temp as() 括号内的内容 就是查询的sql 结果包含重复数据

    row_number() over(partition by 需要检索重复的列 order by 排序的列名)  别名 row_no  ,如果分组存在多条相同值,row_no 从1开始递增,筛选 row_no = 1 ,就取到了只出现一次的数据
     参考:https://blog.csdn.net/u014481096/article/details/82144519

  • 相关阅读:
    LAPACK(5)——矩阵广义特征值问题和QZ分解
    数据结构与算法——堆
    STL(1)——查找函数find的使用
    数据结构与算法——多项式
    LAPACK(6)——总结
    设计模式代理模式
    C#防盗链
    设计模式组合模式
    JavascriptFolder对象
    JavascriptTextStream对象
  • 原文地址:https://www.cnblogs.com/hellohero55/p/12000299.html
Copyright © 2011-2022 走看看