SQL错误:滥用聚合

sqlite版本3.4.0
聚合函数有什么问题?此外,我怀疑ORDER BY也不会奏效.如何重写?
sqlite> SELECT p1.domain_id,p2.domain_id,COUNT(p1.domain_id) AS d1,COUNT(p2.domain_id) AS d2
   ...> FROM PDB as p1,Interacting_PDBs as i1,PDB as p2,Interacting_PDBs as i2
   ...> WHERE p1.id = i1.PDB_first_id
   ...> AND p2.id = i2.PDB_second_id
   ...> AND i1.id = i2.id
   ...> AND d1>100
   ...> AND d2>100
   ...> ORDER BY d1,d2;
sql error: misuse of aggregate: 
sqlite>

解决方法

当使用聚合函数(sum / count / …)时,还必须使用GROUP BY子句.

接下来,当您要过滤汇总的结果时,您不能在WHERE子句中执行此操作,但必须在HAVING子句中执行该操作.

SELECT p1.domain_id,COUNT(p2.domain_id) AS d2
    FROM PDB as p1,Interacting_PDBs as i2
    WHERE p1.id = i1.PDB_first_id
    AND p2.id = i2.PDB_second_id
    AND i1.id = i2.id
GROUP BY p1.domain_Id,p2.domain_Id
HAVING d1 > 100 AND d2 > 100
ORDER BY d1,d2;

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...