• sql分组最大值相关


    房产表tf_estate_card,利润中心组profit_group_code,资产号main_assets_number,原值original_value

    查出每个利润中心组的最大原值及其资产号

    partition by方式:

    select t.main_assets_number, t.profit_group_code, t.original_value
      from tf_estate_card t,
           (select distinct t.profit_group_code,
                            max(t.original_value) over(partition by t.profit_group_code) as max_original_value
              from tf_estate_card t) tmp
     where t.profit_group_code=tmp.profit_group_code and t.original_value=tmp.max_original_value ;

    group by 方式:

    select t.main_assets_number, t.profit_group_code, t.original_value
      from tf_estate_card t,
           (select distinct t.profit_group_code,
                            max(t.original_value) as max_original_value
              from tf_estate_card t group by t.profit_group_code) tmp
     where t.profit_group_code=tmp.profit_group_code and t.original_value=tmp.max_original_value

    rank()/dense_rank() over(partition by e.deptno order by e.sal desc)方式:

    select tmp.main_assets_number, tmp.profit_group_code, tmp.original_value
      from (select t.main_assets_number,
                   t.profit_group_code,
                   t.original_value,
                   rank() over(partition by t.profit_group_code order by t.original_value desc) rank
              from tf_estate_card t) tmp
     where tmp.rank=1

       over:  在什么条件之上。
    partition by e.deptno:  按部门编号划分(分区)。
    order by e.sal desc:  按工资从高到低排序(使用rank()/dense_rank() 时,必须要带order by否则非法)
    rank()/dense_rank():  分级,rank():  跳跃排序,如果有两个第一级时,接下来就是第三级;dense_rank():  连续排序,如果有两个第一级时,接下来仍然是第二级。

  • 相关阅读:
    【转】修改mysql数据库的用户名和密码
    oracle 11g密码过期问题解决方法
    配置网络YUM源
    RedHat 7.0更新升级openSSH7.4p1
    Linux下端口被占用解决
    js function 中的返回值
    代码笔记1
    element模态框dialog中的select组件中选中无反应无显示
    vue如何使用rules对表单字段进行校验
    关于JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑
  • 原文地址:https://www.cnblogs.com/hongfu/p/5235343.html
Copyright © 2020-2023  润新知