-- Top known HTTPS JA4 signatures by number of flows

SELECT
    ja4hash,
    toValidUTF8(ja4desc),
    COUNT(*) AS cnt

FROM (
    SELECT
        ja4hash,
        ja4desc
    FROM
        flow
    ARRAY JOIN
        "sslJA4" AS ja4hash,
        "sslJA4Desc" AS ja4desc
    WHERE
        notEmpty("sslJA4")
        AND notEmpty(ja4desc)
        AND LENGTH(ja4hash) = 36
        AND "timeFirst" >= {time_from:DateTime64(6)}
        AND "timeLast" <= {time_to:DateTime64(6)}
    SETTINGS
        enable_unaligned_array_join = 1
) AS flows

GROUP BY ja4hash, ja4desc
ORDER BY multiply(cnt, {sort_order:Int8})

LIMIT {n:UInt64}
