优化 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;

优化要点:

  1. 使用 COUNT(*) 函数统计每个 CrashIdentifier 出现的次数,并将结果存入 num 列。
  2. 使用 GROUP BY CrashIdentifier, PlatformType, CrashContent2 确保每个 CrashIdentifier 只记录一条数据。
  3. 使用 ORDER BY PlatformType DESCPlatformType 降序排列结果。

解释:

  • COUNT(*) 函数统计每个 CrashIdentifier 出现的次数,并将结果存入 num 列。
  • GROUP BY CrashIdentifier, PlatformType, CrashContent2 确保每个 CrashIdentifier 只记录一条数据,并按照 CrashIdentifier, PlatformType, CrashContent2 进行分组。
  • ORDER BY PlatformType DESCPlatformType 降序排列结果。

通过优化后的 SQL 语句,可以更有效地统计每个 CrashIdentifier 出现的次数,并获得更准确的结果。

优化 SQL 查询:统计每个 CrashIdentifier 出现次数

原文地址: https://www.cveoy.top/t/topic/mz09 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录