The Floor

  • Considering the underlying problem - how do we get the integer value nearest to 0  - I would use cast.
    select cast(123.45 as int), cast(-123.45 as int), cast(123.89 as int), cast(-123.89 as int)
    The above returns 123, -123, 123, -123
    Round, as the name implies, will round to the nearest integer (returning 123, -123, 124, -124) so is less suitable for this task.

    WITH mycte
    AS (
    SELECT 'Coffee' as a, 2.38 as n
    UNION Select 'Coffee', 4.53
    UNION Select 'Recharge card', -15.50
    UNION Select 'Recharge card', -5.25
    )
    SELECT a, SUM(cast(n as int)) cast_n, SUM(FLOOR(n)) floor_n from mycte
    GROUP BY a;

Viewing post 16 (of 15 total)

You must be logged in to reply to this topic. Login to reply