zoukankan      html  css  js  c++  java
  • 四大排序函数

    row_number() over(partition by clause order by filed asc|desc )----连续的从1开始到n的排序号
    [sql] view plain copy
    rank() over ( partition by clause order by filed asc|desc)---不连续的可以出现重复排序号
    [sql] view plain copy
    dense_rank() over (partition by clause order by filed asc|desc)--连续的可以出现重复排序号

    [sql] view plain copy
    ntile (integer_expression) over (partition by clause order by filed asc|desc )--连续的可以分组排序并且排序号是连续的
    [sql] view plain copy
    ----案例

    create table tb (
    [year] [int] NOT NULL,
    [week] [int] NOT NULL,
    [base_goods_id] [int] NOT NULL,
    [uid] [bigint] NOT NULL,
    [amount] [money] NOT NULL,
    [count] [int] NOT NULL,
    [bean] [money] NOT NULL
    )
    insert tb
    select 2013,2,6577,20087,32640.00,1088,26112.00
    union all
    select 2013,2,6577,20211,39420.00,1314,31536.00
    union all
    select 2013,2,6577,20220,60.00,2,111.00
    union all
    select 2013,2,6577,20457,60.00,2,48.00
    union all
    select 2013,2,6577,20458,60.00,2,48.00
    go
    select * from tb
    /*
    2013 2 6577 20087 32640.00 1088 26112.00
    2013 2 6577 20211 39420.00 1314 31536.00
    2013 2 6577 20220 60.00 2 111.00
    2013 2 6577 20457 60.00 2 48.00
    2013 2 6577 20458 60.00 2 48.00
    */

    select ROW_NUMBER() over(partition by base_goods_id order by count)rn,* from tb

    /*
    rn year week base_goods_id uid amount count bean
    1 2013 2 6577 20220 60.00 2 111.00
    2 2013 2 6577 20457 60.00 2 48.00
    3 2013 2 6577 20458 60.00 2 48.00
    4 2013 2 6577 20087 32640.00 1088 26112.00
    5 2013 2 6577 20211 39420.00 1314 31536.00
    */

    select dense_rank()over(partition by base_goods_id order by count )rn,* from tb
    /*
    rn year week base_goods_id uid amount count bean
    1 2013 2 6577 20220 60.00 2 111.00
    1 2013 2 6577 20457 60.00 2 48.00
    1 2013 2 6577 20458 60.00 2 48.00
    2 2013 2 6577 20087 32640.00 1088 26112.00
    3 2013 2 6577 20211 39420.00 1314 31536.00
    */

    select rank()over(partition by base_goods_id order by count )rn,* from tb

    /*
    1 2013 2 6577 20220 60.00 2 111.00
    1 2013 2 6577 20457 60.00 2 48.00
    1 2013 2 6577 20458 60.00 2 48.00
    4 2013 2 6577 20087 32640.00 1088 26112.00
    5 2013 2 6577 20211 39420.00 1314 31536.00
    */

    select ntile(2)over(partition by base_goods_id order by count )rn,* from tb

    /*
    rn year week base_goods_id uid amount count bean
    1 2013 2 6577 20220 60.00 2 111.00
    1 2013 2 6577 20457 60.00 2 48.00
    1 2013 2 6577 20458 60.00 2 48.00
    2 2013 2 6577 20087 32640.00 1088 26112.00
    2 2013 2 6577 20211 39420.00 1314 31536.00
    */

  • 相关阅读:
    HUSTOJ搭建后为了方便作为Judger调用进行的一些修改操作
    [转]我国古代求解最大公约数的方法-更相减损术
    [转]nodejs导出word
    Java抓取Codeforces——针对某一次提交的源码和数据
    Java以UTF-8格式读写及追加写文件示例
    C++使用fill初始化二维数组
    FNV hash算法
    vitess基础镜像构建流程Centos
    go 工具链目前[不支持编译 windows 下的动态链接库]解决方案
    binlog分析方法
  • 原文地址:https://www.cnblogs.com/accumulater/p/6158581.html
Copyright © 2011-2022 走看看