ClickHouse 查询优化:获取最大值并作为变量
SELECT\u0020toStartOfDay(toDateTime(Ds))\u0020AS\u0020datetime_day_Ds,\u0020PlatformType\u0020AS\u0020PlatformType,\u0020max(MergedUrlCount)\u0020\u0020AS\u0020index_mergedviewcount\u0020FROM\u0020log_iMonkey_Wechat_overview\u0020WHERE\u0020Debug\u0020=\u00200\u0020AND\u0020PlatformType\u0020IN\u0020(3,4)\u0020AND\u0020\u0020Ds\u0020>=\u0020toDateTime(#day_wechat_views_time.start#)\u0020\u0020AND\u0020Ds\u0020<=\u0020toDateTime(#day_wechat_views_time.end#)\u0020\u0020GROUP\u0020BY\u0020datetime_day_Ds,\u0020PlatformType\u0020ORDER\u0020BY\u0020datetime_day_Ds\u0020DESC,\u0020PlatformType\u0020DESC\u0020LIMIT\u00205000\u0020如果想从PlatformType等于\u00203\u0020对应的MergedUrlCount和PlatformType等于\u00204\u0020对应的MergedViewCount中每一列取最大值作为变量index_mergedviewcount,怎么改内容:您可以使用以下查询来获取每个日期和平台类型的最大MergedUrlCount和MergedViewCount的值作为变量index_mergedviewcount:\u000a\u000a\u000aSELECT\u0020\u0020toStartOfDay(toDateTime(Ds))\u0020AS\u0020datetime_day_Ds,\u0020\u0020max(MergedUrlCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00203)\u0020AS\u0020MergedUrlCount_PlatformType_3,\u0020\u0020max(MergedUrlCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00204)\u0020AS\u0020MergedUrlCount_PlatformType_4,\u0020\u0020max(MergedViewCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00203)\u0020AS\u0020MergedViewCount_PlatformType_3,\u0020\u0020max(MergedViewCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00204)\u0020AS\u0020MergedViewCount_PlatformType_4,\u0020\u0020GREATEST(\u0020\u0020\u0020max(MergedUrlCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00203),\u0020\u0020\u0020max(MergedUrlCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00204),\u0020\u0020\u0020max(MergedViewCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00203),\u0020\u0020\u0020max(MergedViewCount)\u0020FILTER\u0020(WHERE\u0020PlatformType\u0020=\u00204)\u0020\u0020)\u0020AS\u0020index_mergedviewcount\u0020FROM\u0020log_iMonkey_Wechat_overview\u0020WHERE\u0020Debug\u0020=\u00200\u0020AND\u0020PlatformType\u0020IN\u0020(3,4)\u0020AND\u0020\u0020Ds\u0020>=\u0020toDateTime(#day_wechat_views_time.start#)\u0020AND\u0020Ds\u0020<=\u0020toDateTime(#day_wechat_views_time.end#)\u0020\u0020GROUP\u0020BY\u0020datetime_day_Ds\u0020ORDER\u0020BY\u0020datetime_day_Ds\u0020DESC\u0020LIMIT\u00205000\u0020\u000a\u000a这个查询将为每个日期计算出PlatformType等于3和4的MergedUrlCount和MergedViewCount的最大值,并使用GREATEST函数找出这四个值中的最大值作为变量index_mergedviewcount。
原文地址: https://www.cveoy.top/t/topic/pxCo 著作权归作者所有。请勿转载和采集!