简体   繁体   English

我想按以下两种品质按表订购

[英]I want to order by table with below two qualities

这是我通过查询得到的结果 Above is the result i am getting by my query以上是我通过查询得到的结果

• The output shall be ordered by two different qualities o first, by the number of non-null rankings in descending order o second, by the sum of the rankings, in ascending order • output 应按两种不同的质量排序 o 首先,按非空排名的数量降序排列 o 第二,按排名的总和,升序排列

结果应该是这样的

Above image is for what result should look like上图是结果应该是什么样子

Server used: Mysql使用的服务器:Mysql

this is my query and what i have tried so far any help is greatly appreciated这是我的查询,到目前为止我所尝试的任何帮助都非常感谢

select 
    snippettitle as category
    , if(cntrank>limitnumber,null,cntrank) as counts
    ,if(viewrank>limitnumber,null,viewrank) as views
    , if(likesrank>limitnumber,null,likesrank) likes
    , if(dislikesrank>limitnumber,null,dislikesrank) dislikes
    ,if(commentrank>limitnumber,null,commentrank) comment_count 
from ( select 
            snippettitle 
            ,RANK() OVER (ORDER BY cnt DESC) AS cntrank 
            ,RANK() OVER (ORDER BY views DESC) AS viewrank 
            ,RANK() OVER (ORDER BY likes DESC) AS likesrank 
            ,RANK() OVER (ORDER BY dislikes DESC) AS dislikesrank 
            ,RANK() OVER (ORDER BY comment_count DESC) AS commentrank 
        from homework7a) tmp 
        where 
            (cntrank <= limitnumber 
            || viewrank <= limitnumber 
            || likesrank <= limitnumber 
            || dislikesrank <= limitnumber 
            || commentrank <= limitnumber
    ) 
order by counts desc,views desc,likes desc ,dislikes desc,comment_count desc;

Your 1st condition:你的第一个条件:

by the number of non-null rankings in descending order按降序排列的非空排名数

can be simplified to:可以简化为:

by the number of null rankings in ascending order按null数量升序排列

So I believe that you want this:所以我相信你想要这个:

select *
from yourquery
order by 
  (counts is null) + (views is null) + (likes is null) + (dislikes is null) + (comment_count is null), 
  coalesce(counts, 0) + coalesce(views, 0) + coalesce(likes, 0) + coalesce(dislikes, 0) + coalesce(comment_count, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM