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

人力资源部有他找到的经理。

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

我想为每个 HR 计算他的经理有多少订单,status > 6而女巫没有。

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

4

1 回答 1

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
于 2013-05-08T11:49:30.567 回答