SQL 错误:滥用聚合

编程入门 行业动态 更新时间:2024-10-17 08:25:17
本文介绍了SQL 错误:滥用聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

SQLite 3.4.0 版聚合函数有什么问题?此外,我怀疑 ORDER BY 也不会起作用.如何重写这个?

SQLite version 3.4.0 What's wrong with aggregate functions? Additionally, I suspect that ORDER BY won't work as well. How to rewrite this?

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 子句.

When using an aggregate function (sum / count / ... ), you also have to make use of the GROUP BY clause.

接下来,当你想过滤一个聚合的结果时,你不能在 WHERE 子句中这样做,但你必须在 HAVING 子句中这样做.

Next to that, when you want to filter on the result of an aggregate , you cannot do that in the WHERE clause, but you have to do that in the HAVING clause.

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 GROUP BY p1.domain_Id, p2.domain_Id HAVING d1 > 100 AND d2 > 100 ORDER BY d1, d2;

更多推荐

SQL 错误:滥用聚合

本文发布于:2023-11-22 01:27:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!