子查询不检索所需结果

时间:2018-02-14 14:43:07

标签: sqlite

所以我需要最终计算数据库中原告律师的赢/输百分比

获胜者专栏有3种可能性原告,被告,空白

所以我一直在尝试进行子查询,但没有成功

SELECT plaintiffattorney, state_name, court_name, COUNT(plaintiffattorney) AS "Times Won", 
    (SELECT COUNT(plaintiffattorney) 
    FROM LawyerCase 
    WHERE winner is "Defendant" and filedate >= date('now', '-3 years')) AS "Times Lost"
FROM LawyerCases
WHERE winner is "Plaintiff" and filedate >= date('now', '-3 years') and plaintiffattorney IS NOT ''
GROUP BY plaintiffattorney
ORDER BY COUNT(plaintiffattorney) DESC;

此查询的结果是: query1

每个原告不应该丢失相同的次数(801),有些可能在我指定的时间内没有任何损失

1 个答案:

答案 0 :(得分:0)

子查询计算表中的所有行。

您必须使用外表将correlating限制为属于同一律师的行:

SELECT ...,
       (SELECT COUNT(*)
        FROM LawyerCase AS LC2
        WHERE L2.plaintiffattorney = LawyerCase.plaintiffattorney
          AND ...
       ) AS "Times Lost"
FROM LawyerCase
...