单个查询中的几个条件计数意外地起作用

时间:2013-05-08 09:20:27

标签: sqlite count subquery

SELECT HR.name, (SELECT COUNT(*) WHERE Ord.status > 6), (SELECT COUNT(*) WHERE Ord.status < 6)
        FROM Ord
            JOIN Manager AS Mng ON Mng.person_id = Ord.manager_id
            JOIN Person AS HR ON HR.id = Mng.hr_id 
        GROUP BY HR.id

HR已找到他找到的经理。

"CREATE TABLE Manager (person_id INTEGER PRIMARY KEY NOT NULL, hr_id INTEGER)"

我想为每个人力资源计算他的经理人做出的订单数status > 6和女巫没有订单。

查询我写的上部没有返回正确的结果。 (但返回一些)。

1 个答案:

答案 0 :(得分:0)

您的描述难以理解。

您的查询在子查询中缺少FROM子句和相关性,与>相反的是<=。 你可能想要这样的东西:

SELECT HR.name,
       (SELECT COUNT(*)
        FROM Ord
        WHERE Ord.manager_id IN (SELECT Mng.person_id
                                 FROM Manager as Mng
                                 WHERE Mng.hr_id = HR.id)
          AND Ord.status > 6),
       (SELECT COUNT(*)
        FROM Ord
        WHERE Ord.manager_id IN (SELECT Mng.person_id
                                 FROM Manager as Mng
                                 WHERE Mng.hr_id = HR.id)
          AND Ord.status <= 6),
FROM Person AS HR
GROUP BY HR.id