Skip to content

bigfunctions > median_value

median_value

Call or Deploy median_value ?

✅ You can call this median_value bigfunction directly from your Google Cloud Project (no install required).

  • This median_value function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want to build and manage your own catalog of functions. This is particularly useful if you want to create private functions (for example calling your internal APIs). Discover the framework

Public BigFunctions Datasets:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

median_value(arr)

Description

Return median value of array (inspired from sql-snippets repo)

Examples

1. When array length is odd

select bigfunctions.eu.median_value([1, 4, 3])
select bigfunctions.us.median_value([1, 4, 3])
select bigfunctions.europe_west1.median_value([1, 4, 3])
+-------+
| value |
+-------+
| 3     |
+-------+

2. When array length is even

select bigfunctions.eu.median_value([1, 4, 3, 2])
select bigfunctions.us.median_value([1, 4, 3, 2])
select bigfunctions.europe_west1.median_value([1, 4, 3, 2])
+-------+
| value |
+-------+
| 2.5   |
+-------+

Need help using median_value?

The community can help! Engage the conversation on Slack

For professional suppport, don't hesitate to chat with us.

Found a bug using median_value?

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

For professional suppport, don't hesitate to chat with us.

Use cases

You have a table of users, and each user has a list of scores they've achieved in a game. You want to find the median score for each user.

WITH UserScores AS (
    SELECT 'UserA' AS user_id, [85, 92, 78, 95, 88] AS scores UNION ALL
    SELECT 'UserB' AS user_id, [70, 75, 68, 72, 77] AS scores UNION ALL
    SELECT 'UserC' AS user_id, [90, 95, 88, 92] AS scores
)

SELECT user_id, bigfunctions.us.median_value(scores) AS median_score
FROM UserScores;

This query uses the median_value function to calculate the median score from the scores array for each user. It will return a table like this:

user_id median_score
UserA 88
UserB 75
UserC 91

This is a practical use case where you need to find a representative central value for a set of numbers associated with each row in a table. Other potential use cases include:

  • Sales Analysis: Finding the median sales amount per customer.
  • Financial Modeling: Calculating the median value of a portfolio of investments.
  • Sensor Data Analysis: Determining the median value of readings from a sensor over a period of time.
  • Performance Monitoring: Calculating the median latency of API calls.

In essence, anytime you have an array of numeric data associated with individual records, and you need to find a typical or central value that is robust to outliers, the median_value function becomes very useful.

Spread the word

BigFunctions is fully open-source. Help make it a success by spreading the word!

Share on Add a on