【发布时间】:2020-10-11 17:36:31
【问题描述】:
如何对所有人执行“MySQL”查询 我的列中的值?
这是我的桌子
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";
如何针对我的“马”列中的所有值调整此查询。 谢谢你的帮助。。
【问题讨论】:
-
幸运的是,您的查询已经在使用聚合函数,因此您只需将
where horse = '...'替换为group by horse -
考虑处理应用代码中数据显示的问题
标签: mysql sql select average window-functions