优化 SQL 查询:统计每个 CrashIdentifier 出现次数
优化 SQL 查询:统计每个 CrashIdentifier 出现次数
以下 SQL 查询语句旨在统计每个 CrashIdentifier 在过去一天内出现的次数,并将结果按 PlatformType 降序排列。
优化前的 SQL 语句:
SELECT CrashIdentifier, PlatformType, CrashContent2
FROM (
select CrashIdentifier,PlatformType,CrashContent2 from (
select CrashIdentifier,PlatformType,CrashContent2, min(Ds) as Ds
from log_iMonkey_Wechat_overview
GROUP BY CrashIdentifier,PlatformType,CrashContent2
) t1
where toDate(Ds) >= subtractDays(toDate(now()), 1) AND toDate(Ds) < toDate(now())
)
GROUP BY PlatformType, CrashIdentifier, CrashContent2
ORDER BY PlatformType DESC;
优化后的 SQL 语句:
SELECT CrashIdentifier, PlatformType, CrashContent2, COUNT(*) AS num
FROM (
SELECT CrashIdentifier, PlatformType, CrashContent2
FROM (
SELECT CrashIdentifier, PlatformType, CrashContent2, MIN(Ds) AS Ds
FROM log_iMonkey_Wechat_overview
GROUP BY CrashIdentifier, PlatformType, CrashContent2
) t1
WHERE toDate(Ds) >= subtractDays(toDate(now()), 1) AND toDate(Ds) < toDate(now())
)
GROUP BY CrashIdentifier, PlatformType, CrashContent2
ORDER BY PlatformType DESC;
优化要点:
- 使用
COUNT(*)函数统计每个CrashIdentifier出现的次数,并将结果存入num列。 - 使用
GROUP BY CrashIdentifier, PlatformType, CrashContent2确保每个CrashIdentifier只记录一条数据。 - 使用
ORDER BY PlatformType DESC按PlatformType降序排列结果。
解释:
COUNT(*)函数统计每个CrashIdentifier出现的次数,并将结果存入num列。GROUP BY CrashIdentifier, PlatformType, CrashContent2确保每个CrashIdentifier只记录一条数据,并按照CrashIdentifier,PlatformType,CrashContent2进行分组。ORDER BY PlatformType DESC按PlatformType降序排列结果。
通过优化后的 SQL 语句,可以更有效地统计每个 CrashIdentifier 出现的次数,并获得更准确的结果。
原文地址: https://www.cveoy.top/t/topic/mz09 著作权归作者所有。请勿转载和采集!