ipython-sql icon indicating copy to clipboard operation
ipython-sql copied to clipboard

Double-nested query returns too few results

Open darkobodnaruk opened this issue 8 years ago • 0 comments

This (somewhat complicated) query returns the wrong results from Postgres.

Compared to the exact same query through postgres' command-line tool, some months are missing altogether. To me, that would indicate that one or both of the inner queries get limited, even though I never set the SqlMagic.autolimit setting.

SELECT country, month, avg(days) AS avg_days, avg(ratio) AS avg_per_active_day
FROM
    (SELECT country, month, device_uuid, count(day) AS days, sum(s) AS opens, sum(s)::FLOAT / count(day) AS ratio
    FROM
        (SELECT 
            country,
            date_trunc('month', date)::DATE AS month,
            date_trunc('day', date)::DATE AS day,
            event_daily_counts.device_uuid,
            sum(count) AS s
        FROM event_daily_counts 
        WHERE date >= '2016-01-01' AND DATE <= '2016-12-31' 
        GROUP BY 1, 2, 3, 4
        ORDER BY 1, 2, 3, 4) AS t
    GROUP BY country, month, device_uuid) AS t2
GROUP BY country, month
ORDER BY country, month

darkobodnaruk avatar Feb 07 '17 14:02 darkobodnaruk