如何执行此命令;MySQL“;查询“我的列”中的所有值;马。。。这里是我的列中的查询Execute 1值;马;
如何为所有用户执行“MySQL”查询 我列中的值 这是我的桌子如何执行此命令;MySQL“;查询“我的列”中的所有值;马。。。这里是我的列中的查询Execute 1值;马;,mysql,sql,select,average,window-functions,Mysql,Sql,Select,Average,Window Functions,如何为所有用户执行“MySQL”查询 我列中的值 这是我的桌子 Table A |----------------------|---------------------|------------------| | id | CL | horse | |----------------------|---------------------|------------------| |
Table A
|----------------------|---------------------|------------------|
| id | CL | horse |
|----------------------|---------------------|------------------|
| 1 | 1er | C.Ferland |
| 2 | 5e | Abrivard |
| 3 | 3e | P. Hawas |
|----------------------|---------------------|------------------|
我希望输出为:
+------------+--------+---------+---------+-----------+
| horse | Top_1 | Top_2_3 | TOP_4_5 | TOP_total |
+------------+--------+---------+---------+-----------+
| C. Ferland | 0.1757 | 0.2788 | 0.1892 | 0.6436 |
| Abrivard | 0.0394 | 0.1231 | 0.1575 | 0.3199 |
| P. Hawas | 0.0461 | 0.1263 | 0.1092 | 0.2816 |
+------------+--------+---------+---------+-----------+
目前,我正在运行这个查询,查找我的horse列中的一个值。
这很有效
SELECT horse,
sum(case when `cl` = '1er' then 1 else 0 end)/count(*) as Top_1,
sum(case when `cl` BETWEEN 2 AND 3 then 1 else 0 end)/count(*) as Top_2_3,
sum(case when `cl` BETWEEN 4 AND 5 then 1 else 0 end)/count(*) as TOP_4_5,
sum(case when `cl` BETWEEN 1 AND 5 then 1 else 0 end)/count(*) as TOP_total
FROM p_mu.cachedate
WHERE horse ="C.Ferland";
如何对我的“horse”列中的所有值调整此查询。
感谢您的帮助。您可以使用条件聚合,但您想要的结果表明,您想要计算整个数据集的平均值,而不仅仅是组中的行。窗口功能在这方面很方便:
select
horse,
sum(cl = 1) / count(*) over() top_1,
sum(cl in (2,3)) / count(*) over() top_2_3,
sum(cl in (4,5)) / count(*) over() top_4_5,
sum(cl <= 5) / count(*) over() top_1_5
from p_mu.cachedate
group by horse
选择
马,
总和(cl=1)/计数(*)超过()顶部,
(2,3)中的和(cl)/count(*)over()top\u 2\u 3,
和(4,5)中的cl/count(*)超过()顶部的4,5,
sum(cl)幸运的是,您的查询已经在使用聚合函数,因此您可以将where horse='…'
替换为groupby horse
考虑处理应用程序代码中的数据显示问题。很好,这正是我想要的;谢谢
select *
from (
select
horse,
sum(cl = 1) / count(*) over() top_1,
sum(cl in (2,3)) / count(*) over() top_2_3,
sum(cl in (4,5)) / count(*) over() top_4_5,
sum(cl <= 5) / count(*) over() top_1_5
from p_mu.cachedate
group by horse
) t
where horse = 'C.Ferland'
select
horse,
sum(cl = 1) / x.cnt top_1,
sum(cl in (2,3)) / x.cnt top_2_3,
sum(cl in (4,5)) / x.cnt top_4_5,
sum(cl <= 5) / x.cnt top_1_5
from p_mu.cachedate
inner join (select count(*) cnt from p_mu.cachedate) x
group by horse