• Actually, the issue is absolutely valid and comes from the sequence of the query execution.

    HAVING is executed after GROUP BY and is applied to the values calculated by the GROUP BY.

    Those values have to be stored somewhere, and the engine has to allocate some memory for that.

    INSERT - SELECT query, unlike simple SELECT, has the data type of the target data field defined, so the calculations of the [diff] value happen within int data type limits.

    Pretty natural for a language with data type control. Just need to keep in mind that the INSERT is an internal part of the query and it affects the rest of it.

    _____________
    Code for TallyGenerator