簡體   English   中英

一列的 SQL AVG 值,其中另一列等於特定值

[英]SQL AVG value of one column where another column equal specific value

我得到了投手、投球類型和投球速度的數據。

|------------------------------------------------|
| day | inning | pitcher| pitch_type| pitch_speed|
|  1       1        AE1     fastball|      97    |
|  1       1        AE1     fastball|      94    |
|  1       1        AE1      slider |      83    |
|  1       2        AE1     fastball|      96    |
|  1       2        AE1      slider |      86    |
|  1       2        AE1     fastball|      97    |
|------------------------------------------------|

有沒有辦法查詢數據以獲取特定螺距類型的螺距速度的平均值。

IE 一種返回 fastball_speed = 96 和 slider_speed = 84.5(平均值)的方法

那這個呢?

select pitch_type, avg(pitch_speed) from your_table group by pitch_type

順便說一句,請在指定示例數據時,使用 CTE 使求解器的工作更輕松:

#standardSql
with t as (
select   1 as day,      1 as inning,       'AE1' as pitcher,     'fastball' as pitch_type,    97 as pitch_speed union all
select   1 as day,      1 as inning,       'AE1' as pitcher,     'fastball' as pitch_type,    94 as pitch_speed union all
select   1 as day,      1 as inning,       'AE1' as pitcher,     'slider'   as pitch_type,    83 as pitch_speed union all
select   1 as day,      2 as inning,       'AE1' as pitcher,     'fastball' as pitch_type,    96 as pitch_speed union all
select   1 as day,      2 as inning,       'AE1' as pitcher,     'slider'   as pitch_type,    86 as pitch_speed union all
select   1 as day,      2 as inning,       'AE1' as pitcher,     'fastball' as pitch_type,    97 as pitch_speed
)
select pitch_type, avg(pitch_speed) from t group by pitch_type

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM