1

我有一个查询:

SELECT MIN(stat_md5) AS stat_md5 
FROM 
     (select * from ( mem_stat NATURAL LEFT OUTER JOIN host ))
GROUP BY stat_md5;

此查询在SQLite version中运行良好3.4.2,但是当我升级到 version 时3.7.11,出现以下错误:

SQL error: aggregate functions are not allowed in the GROUP BY clause

如果我从查询中删除MIN()or GROUP BYor AS,那么它在 version 中工作正常3.7.11

这意味着以下查询变体工作正常:

SELECT MIN(stat_md5) AS stat_md5 
FROM 
     (select * from ( mem_stat NATURAL LEFT OUTER JOIN host ))


SELECT MIN(stat_md5) 
FROM 
     (select * from ( mem_stat NATURAL LEFT OUTER JOIN host ))
GROUP BY stat_md5;


SELECT stat_md5 
FROM 
     (select * from ( mem_stat NATURAL LEFT OUTER JOIN host ))
GROUP BY stat_md5;

有人可以解释为什么它在版本 3.7.11 中给出错误及其解决方法。

更新:

这是原始查询。

SELECT 
      stat_day, obs_day, stat_week, obs_week, 
      NULL AS stat_cputotal, stat_memfree, stat_md5,
      stat_month, obs_month, stat_type, stat_memused, stat_10min, obs_10min, 
      NULL AS stat_cpusystem, stat_epoch, obs_epoch, stat_30day, obs_30day, 
      stat_idx, obs_idx, NULL AS stat_cpuidle, stat_memtotal, stat_qtr, obs_qtr,
      NULL AS stat_cpuuser, stat_hour, obs_hour, obs_10min, stat_10min, 
      obs_30day,stat_30day, obs_day, stat_day, obs_epoch, stat_epoch, obs_hour,
      stat_hour, obs_idx, stat_idx, obs_month, stat_month, obs_qtr, stat_qtr, 
      obs_week, stat_week, serial_num, NULL AS dputil_cpuid, NULL AS wms_pk 
      FROM (
       (
       SELECT stat_qtr, obs_qtr, stat_qtr AS cus_qtr, stat_qtr AS crash_qtr, 
              stat_qtr AS evt_qtr, stat_30day, obs_30day, stat_30day AS cus_30day, 
              stat_30day AS crash_30day, stat_30day AS evt_30day, stat_month, 
              obs_month, stat_month AS cus_month, stat_month AS crash_month, 
              stat_month AS evt_month, stat_week, obs_week, stat_week AS cus_week, 
              stat_week AS crash_week, stat_week AS evt_week, stat_day, obs_day, 
              stat_day AS cus_day, stat_day AS crash_day, stat_day AS evt_day, 
              stat_hour, obs_hour, stat_hour AS cus_hour, stat_hour AS crash_hour, 
              stat_hour AS evt_hour, stat_10min, obs_10min, stat_10min AS cus_10min, 
              stat_10min AS crash_10min, stat_10min AS evt_10min, stat_epoch, 
              obs_epoch, stat_epoch AS cus_epoch, stat_epoch AS crash_epoch, 
              stat_epoch AS evt_epoch, stat_idx, obs_idx, stat_idx AS cus_idx, 
              stat_idx AS crash_idx, stat_idx AS evt_idx, obs_10min, obs_30day, 
              obs_day, obs_epoch, obs_hour, obs_idx, obs_month, obs_qtr, obs_week, 
              serial_num, stat_md5, MIN(stat_memfree) AS stat_memfree, MIN(stat_md5) 
              AS stat_md5, MIN(stat_type) AS stat_type, MIN(stat_memused) AS 
              stat_memused, MIN(stat_memtotal) AS stat_memtotal 
              FROM 
                   (select * from ( mem_stat NATURAL LEFT OUTER JOIN host )) 
                    GROUP BY 
                            stat_qtr, stat_30day, stat_month, stat_week, 
                            stat_day, stat_hour, stat_10min, stat_epoch, 
                            stat_idx, obs_10min,obs_30day, obs_day, obs_epoch, 
                            obs_hour, obs_idx, obs_month, obs_qtr, obs_week, 
                            serial_num, stat_md5
                   ))
4

2 回答 2

1

, stat_md5, MIN(stat_memfree) AS stat_memfree, MIN(stat_md5) AS stat_md5,

stat_md5 在 SELECT 列表中出现了两次:一次单独出现,一次作为 MIN() 的别名。3.4.2 必须使用第一个,3.7.11 必须使用第二个。

按聚合字段分组没有意义,因此错误是正确的。

只需将第二次的别名更改为类似的名称AS min_stat_md5,它就会正常工作。在任何情况下,在 SELECT 上拥有两个具有相同名称的字段都是一个坏主意。

于 2012-07-30T14:37:57.890 回答
0

这个内联视图中的括号对我来说看起来很时髦:

       (select * from ( mem_stat NATURAL LEFT OUTER JOIN host ))  

此外,IMO,为了查询的清晰和可读性,最好为内联视图分配别名:

      select Books4Kids.Price
      from
      (
       select authors.name, titles.price
       from authors inner join titles on authors.authorid = titles.authorid
       where titles.category = 'Childrens Literature'
      ) as Books4Kids
于 2012-07-30T18:35:14.013 回答