从2个日期之间的单独表中计算2列

时间:2017-04-06 21:32:54

标签: sqlite

我有一个查询,使用子查询计算2个独立表中的2列,这有效。现在,我必须在此查询中实现根据呼叫记录的日期过滤掉这些结果的能力。我将发布我正在使用的查询:

    FirefoxOptions ffOptions = new FirefoxOptions();
    options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //Location where Firefox is installed

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("moz:firefoxOptions", options);

    FirefoxDriver driver = new FirefoxDriver(capabilities);
    driver.get("http://www.google.com");

好的,所以表调用存储日期,其PK是CallID。 CallToLineOfficers和CallToMembers都是桥表,它们也只包含CallID和MemberID。使用当前查询,日期被注释掉,该日期范围应仅返回所有名称,但计数1应出现在1个人的名下。

我尝试将Call.CallID加入两个桥表' CallID没有任何运气,但我认为这是正确的方法。有人能指点我正确的方向吗?我搞不清楚了。 (我尽力解释这个问题,所以如果你需要更多信息,请告诉我。)

更新:这是我得到的截图:

Count

根据样本中提供的日期,带有日期的新结果应为:

SELECT (m.FirstName || " " || m.LastName) AS Members,
   (
       SELECT count(CallToLineOfficers.MemberID) 
         FROM CallToLineOfficers
        WHERE CallToLineOfficers.MemberID = m.MemberID
   )
+  (
       SELECT count(CallToMembers.MemberID) 
         FROM CallToMembers
        WHERE CallToMembers.MemberID = m.MemberID
   ) AS Tally
FROM Members AS m, Call, CallToMembers, CallToLineOfficers
Join Call on CallToMembers.CallID = Call.CallID
and CallToLineOfficers.CallID = Call.CallI
WHERE m.FirstName <> 'None'
-- and Call.Date between '2017-03-21' and '2017-03-22'
GROUP BY m.MemberID
ORDER BY m.LastName ASC;

1 个答案:

答案 0 :(得分:1)

目前,子查询仅过滤成员ID。因此,对于外部查询中的任何成员ID,它们将返回完整计数。

要减少计数,您必须在子查询中进行过滤:

SELECT (FirstName || " " || LastName) AS Members,
   (
       SELECT count(*)
         FROM CallToLineOfficers
         JOIN Call USING (CallID)
        WHERE MemberID = m.MemberID
          AND Date BETWEEN '2017-03-21' AND '2017-03-22'
   )
+  (
       SELECT count(*)
         FROM CallToMembers
         JOIN Call USING (CallID)
        WHERE MemberID = m.MemberID
          AND Date BETWEEN '2017-03-21' AND '2017-03-22'
   ) AS Tally
FROM Members AS m
WHERE FirstName <> 'None'
ORDER BY LastName ASC;