WITH filtered_times AS (
    SELECT
        COALESCE(
            NULLIF(rsa_time,0),
            (SELECT MAX(rsa_time) FROM dtf.madelines_files_results)
        ) AS rsa_time,
        COALESCE(
            NULLIF(hyper_pulse_time,0),
            (SELECT MAX(hyper_pulse_time) FROM dtf.madelines_files_results)
        ) AS hyper_pulse_time,
        COALESCE(
            NULLIF(quantum_x_time,0),
            (SELECT MAX(quantum_x_time) FROM dtf.madelines_files_results)
        ) AS quantum_x_time,
        COALESCE(
            NULLIF(aes_time,0),
            (SELECT MAX(aes_time) FROM dtf.madelines_files_results)
        ) AS aes_time,
        COALESCE(
            NULLIF(d_crypt_time,0),
            (SELECT MAX(d_crypt_time) FROM dtf.madelines_files_results)
        ) AS d_crypt_time
    FROM dtf.madelines_files_results
)

SELECT
    AVG(rsa_time)::numeric(10,2) AS avg_rsa_time,
    AVG(hyper_pulse_time)::numeric(10,2) AS avg_hyper_pulse_time,
    AVG(quantum_x_time)::numeric(10,2) AS avg_quantum_x_time,
    AVG(aes_time)::numeric(10,2) AS avg_aes_time,
    AVG(d_crypt_time)::numeric(10,2) AS avg_d_crypt_time
FROM filtered_times
