lassence.net

Field notes from the AI frontier

Compute percent of column

--- archive: codebook, 2020–2023 · topic: SQL ---

In SQL, to get the percentage of a row over the column total, you can use a window function to sum the column, and then divide each row by this total:

SELECT
    wait_time,
    COUNT(*) AS n,
    n*1.0 / SUM(n) OVER () AS pct
FROM rides
GROUP BY 1
ORDER BY 1