RosettaCodeData/Task/Averages-Mode/DuckDB/averages-mode-5.duckdb
2025-08-11 18:05:26 -07:00

15 lines
445 B
Text

# Find the mode of a column named values in the named table
# by taking the median() of the modal values
create or replace function mode_of_column(TableName) as (
with cte as (
SELECT value, COUNT(*) as frequency
FROM query_table(TableName)
GROUP BY value
),
modal_values as (
SELECT value
from cte
where frequency = (SELECT MAX(frequency) FROM cte)
order by value
) select median(value) from modal_values
);