SQL Query Optimization: Monotonically Increasing Total Count with Date
To ensure that the total_count calculated by SUM(cnt) is a monotonically increasing value with the increase of Ds, you can modify the code as follows:\n\nSELECT SUM(cnt) AS total_count, Ds\nFROM (\n SELECT COUNT(DISTINCT AppIdentifier) AS cnt, Ds\n FROM log_iMonkey_iOS_overview\n GROUP BY Ds\n UNION ALL\n SELECT COUNT(DISTINCT AppIdentifier) AS cnt, Ds\n FROM log_iMonkey_Android_overview\n WHERE PlatformType != 0 AND AppIdentifier != 'identifier'\n GROUP BY Ds\n UNION ALL\n SELECT COUNT(DISTINCT PlatformType) AS cnt, Ds\n FROM log_iMonkey_Wechat_overview\n WHERE PlatformType != 0 AND Ds <= 'current_date'\n GROUP BY Ds\n) AS subquery\nGROUP BY Ds\nORDER BY Ds ASC; \n\nIn the last UNION ALL subquery, I added a condition AND Ds <= 'current_date'
to filter the rows with Ds less than or equal to the current date. This ensures that only the counts of PlatformType up to the current date are included in the calculation of total_count.\n\nAlso, I added an ORDER BY clause to sort the result by Ds in ascending order to demonstrate the monotonically increasing total_count.
原文地址: http://www.cveoy.top/t/topic/pQ1P 著作权归作者所有。请勿转载和采集!