gpt4 book ai didi

mysql : ORDER BY Twice

转载 作者:行者123 更新时间:2023-11-29 10:27:43 27 4
gpt4 key购买 nike

已编辑:我需要用户按其全局进度 [order by avg(progression) DESC] 以及按进度排序的详细信息行进行排序。

示例:

USER 1 campaign 1 progression 100 
USER 1 campaign 2 progression 100
USER 2 campaign 1 progression 95
USER 2 campaign 2 progression 80
USER 3 campaign 1 progression 70
USER 3 campaign 2 progression 25

我运行以下查询:

SELECT p.usersid, p.current_campaign, p.campaign_progression
FROM progression_campagne p
JOIN USER u ON p.usersid=u.id AND u.organismsid=10
GROUP BY p.usersid, p.current_campaign
ORDER BY p.usersid DESC,p.campaign_progression DESC;

要得到这样的结果:

enter image description here

但是我得到了以下结果:

enter image description here

有什么帮助吗?

最佳答案

添加平均列并将其用作第一个排序列

drop table if exists t;
create table t(user int, campaign int , progression int);
insert into t values
(1 , 1 , 100) ,
(1 , 2 , 100),
(2 , 1 , 95 ),
(2 , 2 , 80),
(3 , 1 , 70 ),
(3 , 2 , 25);

select user,campaign, progression, (select avg(t1.progression) from t t1 where t1.user = t.user group by t1.user) average
from t
order by average desc,user desc,progression desc;

+------+----------+-------------+----------+
| user | campaign | progression | average |
+------+----------+-------------+----------+
| 1 | 1 | 100 | 100.0000 |
| 1 | 2 | 100 | 100.0000 |
| 2 | 1 | 95 | 87.5000 |
| 2 | 2 | 80 | 87.5000 |
| 3 | 1 | 70 | 47.5000 |
| 3 | 2 | 25 | 47.5000 |
+------+----------+-------------+----------+

关于mysql : ORDER BY Twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com