简体   繁体   中英

How do I incorporate FLOAT in my query when calculating the average of a set of values when my result could be a FLOAT and not INTEGER to avoid error

I am trying to determine the average of a set of values from a table but I am getting an error.

I entered the query below to determine the average riderships from 2013 - 2016 but got this error - No matching signature for operator / for argument types: STRUCT<INT64, INT64>, INT64. Supported signatures: FLOAT.

SELECT
station_name,
ridership_2013,
ridership_2014,
ridership_2015,
ridership_2016,
(ridership_2013 + ridership_2014 + ridership_2015, ridership_2016) / 4 AS Average_ridership

FROM bigquery-public-data.new_york_subway.subway_ridership_2013_present;

Use CAST to change the data types to INT64.

SELECT
station_name,
CAST (ridership_2013 AS INT64),
CAST (ridership_2014 AS INT64,
CAST (ridership_2015 AS INT64),
CAST (ridership_2016 AS INT64),
(CAST(ridership_2013 AS INT64) + CAST(ridership_2014 AS INT64) + CAST(ridership_2015) + CAST(ridership_2016 AS INT64) / 4 AS Average_ridership

FROM bigquery-public-data.new_york_subway.subway_ridership_2013_present;

You simply has a typo! try below

SELECT
station_name,
ridership_2013,
ridership_2014,
ridership_2015,
ridership_2016,
(ridership_2013 + ridership_2014 + ridership_2015 + ridership_2016) / 4 AS Average_ridership
FROM bigquery-public-data.new_york_subway.subway_ridership_2013_present;    

as you can see - the typowas a comma instead of plus sign in ridership_2015, ridership_2016

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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