Skip to content

median_value

median_value(arr)

Description

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

Usage

Call or Deploy median_value ?
Call median_value directly

The easiest way to use bigfunctions

  • median_value function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy median_value in your project

Why deploy?

  • You may prefer to deploy median_value in your own project 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).
  • Get started by reading the framework page

Deployment

median_value function can be deployed with:

pip install bigfunctions
bigfun get median_value
bigfun deploy median_value

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   |
+-------+

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.


Need help or Found a bug?
Get help using median_value

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about 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.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on