median_approx - BigQuery

Function

median_approx(col)

Description

Median Approx - Finds the median of the specified column <col> by calculating approximate quantiles (1% granularity approximation). This approach provides an efficient and fast estimate of the median value.

Example Query


WITH sample AS (
SELECT d FROM UNNEST(ARRAY[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39]) AS d

)
SELECT `justfunctions.eu.median_approx`(d)
FROM sample
                                            
/*--Output--
20
*/

Statement

CREATE OR REPLACE AGGREGATE FUNCTION `your_project_id.your_dataset_id.median_approx`(`col` float64) 
  RETURNS float64 AS ((APPROX_QUANTILES(col, 100))[OFFSET(50)])
  OPTIONS ( description = '''Finds the median of the specified column <col> by calculating approximate quantiles (1% granularity approximation). This approach provides an efficient and fast estimate of the median value.''')

Regions

justfunctions.eu.median_approx(col),
justfunctions.us.median_approx(col)

Type

SQL User Defined Aggregate Function (SQL UDAF)

How to Use

Frequently Asked Questions

User-Defined Functions (UDFs) in Google BigQuery are custom functions that you can create to perform operations that aren't available through the standard SQL functions. These UDFs allow you to extend BigQuery's SQL capabilities to suit your specific data processing needs. JustFunctions is a collection of open-source user-defined functions (UDFs).

JustFunctions is a collection of open-source User-Defined Functions (UDFs) designed to extend the capabilities of Google BigQuery. These functions cover a wide range of applications, including text manipulation, URL processing, date processing, email handling, similarity measures, and more. Moreover, JustFunctions is frequently updated to include more use cases.

We welcome any feedback or questions you may have. You can Contact us or report an issue on Github.

Functions and procedures from JustFunctions can be used directly in any of your projects.
To start, simply click 👆 on any function,  Copy  the 'Example Query' and run it in your BigQuery console.
You can also  Copy  the 'Statement' to create your own private user-defined function.

Yes, JustFunctions is completely free to use.

Yes, currently JustFunctions is only available for Google BigQuery. In the future, we will also support PostgreSQL.

See something wrong? Contact us or report an issue on Github.