我有一个查询,在 SQL db 上运行时输出以下结果
CreatedDate QuoteNumber NumberOfLicensedDrivers
2012-07-01 10:38:49.157 48641 0
2012-07-01 18:35:38.680 48650 2
2012-07-02 08:44:33.770 48670 1
2012-07-02 09:12:09.447 48700 0
为了这篇文章,我已经简化了查询,但基本上是:
SELECT DISTINCT
CreatedDate,
QuoteNumber,
count([DriversLicense]) as NumberOfLicensedDrivers
FROM table1 LEFT OUTER JOIN table2 on table2.QuoteID = table1.ID
WHERE CreatedDate > '7/1/2012'
GROUP BY QuoteNumber, CreatedDate
ORDER BY CreatedDate ASC
这个查询给了我预期的输出。我希望所有引号都显示在我的输出中。如果有 0 个驱动程序,我希望显示 0 个。问题是我需要使用链接表在 MS 访问中创建相同的结果。MS 访问输出没有给我预期的结果。
这是我用于 MS ACCESS 的查询
SELECT DISTINCT
quote.CreatedDate,
quote.QuoteNumber,
count(quoteDrivers.DriversLicense) As NumberOfLicensedDrivers
FROM quote
LEFT JOIN quoteDrivers ON quote.ID = quoteDrivers.QuoteID
WHERE (quote.CreatedDate>#7/1/2012#)
GROUP BY quote.CreatedDate, quote.QuoteNumber
ORDER BY quote.CreatedDate;
输出显示没有许可的驱动程序:
CreatedDate QuoteNumber NumberOfLicensedDrivers
7/1/2012 10:38:49 AM 48641 0
7/1/2012 6:35:39 PM 48650 0
7/2/2012 8:44:34 AM 48670 0
7/2/2012 9:12:09 AM 48700 0
但是,当我对 quoteDrivers 表添加一个额外的 WHERE 子句时:
SELECT DISTINCT
quote.CreatedDate,
quote.QuoteNumber,
count(quoteDrivers.DriversLicense) As NumberOfLicensedDrivers
FROM quote
LEFT JOIN quoteDrivers ON quote.ID = quoteDrivers.QuoteID
WHERE (quote.CreatedDate>#7/1/2012#)
and quoteDrivers.DriverAddedDate >#1/1/2012# ''Added this
GROUP BY quote.CreatedDate, quote.QuoteNumber
ORDER BY quote.CreatedDate;
我得到了预期的结果(减去 0 个驱动程序的引号)
CreatedDate QuoteNumber NumberOfLicensedDrivers
7/1/2012 6:35:39 PM 48650 2
7/2/2012 8:44:34 AM 48670 1
谁能解释为什么我没有在 quoteDrivers 表的 where 子句中指定列的情况下没有得到驱动程序的数量?