SQL error: misuse of aggregate

后端 未结 3 1621
夕颜
夕颜 2020-12-03 02:46

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> SELE         


        
相关标签:
3条回答
  • 2020-12-03 03:28

    Also make sure that of you don't use one of the columns that you are using COUNT/SUM for a WHERE clause. Make sure you use HAVING instead of WHERE for those variables.

    0 讨论(0)
  • 2020-12-03 03:39

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

    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;
    
    0 讨论(0)
  • 2020-12-03 03:43

    Short-version fix for this is:

    When you're using function like COUNT/SUM, you need to use HAVING instead of WHERE.

    0 讨论(0)
提交回复
热议问题