• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    查询数据排名情况SQL
    POST TIME:2021-10-18 19:07

    1/准备测试数据

    ---------------------------------------------------------------------------------
    create table t1(
    c1 integer,
    c2 integer,
    c3 integer
    );

    insert into t1 values(1,2,3)

    insert into t1 values(1,8,4)
    insert into t1 values(1,4,4)

    insert into t1 values(1,4,5)

    insert into t1 values(1,5,5)

    insert into t1 values(2,2,3)

    insert into t1 values(2,8,4)
    insert into t1 values(2,4,4)

    insert into t1 values(2,4,5)

    insert into t1 values(2,5,5)

    2/查看排名

    ---------------------------------------------------------------------------------

    A/单记录排名

    select c1,c3,
    (select count( c3)+1 from t1 a where a.c3>b.c3
    and a.c1=b.c1 and a.c1 =1
    ) order_num
    from t1 b
    where  c1 =1
    order by c1,c3

    c1          c3          order_num             
    ----------- ----------- ----------------------
    1           3           5                     
    1           4           3                     
    1           4           3                     
    1           5           1                     
    1           5           1     
    B/多记录排名

    select c1,c2,c3,
    (select count( c3)+1 from t1 a where a.c3>b.c3
    and a.c1=b.c1
    ) order_num
    from t1 b
    order by c1,c3


    c1          c2          c3          order_num             
    ----------- ----------- ----------- ----------------------
    1           2           3           5                     
    1           8           4           3                     
    1           4           4           3                     
    1           4           5           1                     
    1           5           5           1                     
    2           2           3           5                     
    2           8           4           3                     
    2           4           4           3                     
    2           4           5           1                     
    2           5           5           1  

     

    您可能感兴趣的文章:
    • 只有两个字段用一个sql语句查询出某个学生的姓名、成绩以及在表中的排名
    • MySQL中给自定义的字段查询结果添加排名的方法
    • SQL查询排名函数实例
    上一篇:Sql Server 数据库索引整理语句,自动整理数据库索引
    下一篇:sqlserver 比较两个表的列
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信