-- Top DNS IPv6 address by number of flows

SELECT
    ip,
    COUNT(*) AS cnt

FROM (
    SELECT
        ip
    FROM
        flow
    ARRAY JOIN
        "dns6Aaddress" AS ip
    WHERE
        notEmpty("dns6Aaddress")
        AND bitTest("dnsStat", 1) = 0  -- Ignore NBNS
        AND "timeFirst" >= {time_from:DateTime64(6)}
        AND "timeLast" <= {time_to:DateTime64(6)}
) AS flows

WHERE ip != IPv6StringToNum('::')

GROUP BY ip
ORDER BY cnt DESC

LIMIT {n:UInt64}
