有没有人知道Access VBA SQL在中间日期执行多重计数功能的方法?
例如,日期表:
01/16/2014 +1 To Inbetween Dates
01/17/2014 +1 to Inbetween Dates
01/13/2014 +1 to Inbetween Dates
01/01/2014 +1 to Not Inbetween Dates
01/02/2014 +1 to not inbetween dates
Start Date | End Date
01/13/2014 | 01/18/2014
最终结果应该是 不,中间= 3 不。在中间= 2
感谢您的帮助!
答案 0 :(得分:2)
您的查询将如下所示:
SELECT SUM(IIF([Date] BETWEEN [Enter Start Date] AND [Enter End Date], 1, 0)) AS BetweenDates,
SUM(IIF([Date] BETWEEN [Enter Start Date] AND [Enter End Date], 0, 1)) AS NotBetweenDates
FROM T;
然后在运行查询时,系统会提示您输入开始日期和结束日期。
如果您只想对日期进行硬编码,那么它将类似于:
SELECT SUM(IIF([Date] BETWEEN #2014-01-13# AND #2014-01-18#, 1, 0)) AS BetweenDates,
SUM(IIF([Date] BETWEEN #2014-01-13# AND #2014-01-18#, 0, 1)) AS NotBetweenDates
FROM T;
编辑(回应评论)
我多年来一直没有使用过访问权限,也没有使用过几百行的任何东西,对于任何更大的东西,我总是看着SQL Server,所以不能说这是多么正常。 但是你正在选择所有结果,所以你需要进行全表扫描,但是我认为这可能表现得更好的唯一方法就是你在日期字段中添加一个索引并执行以下操作:
SELECT 'Between Dates' AS Status,
COUNT(*) AS Records
FROM T
WHERE [Date] BETWEEN #2014-01-13# AND #2014-01-18#
UNION ALL
SELECT 'Not Between Dates' AS Status,
COUNT(*) AS Records
FROM T
WHERE [Date] NOT BETWEEN #2014-01-13# AND #2014-01-18#;
或者:
SELECT IIF([Date] BETWEEN #2014-01-13# AND #2014-01-18#, 'Between', 'Not Between') AS Status,
COUNT(*) AS Records
FROM T
GROUP BY IIF([Date] BETWEEN #2014-01-13# AND #2014-01-18#, 'Between', 'Not Between');
但由于没有数据可供测试,我无法确定这是否有帮助。
答案 1 :(得分:0)
以下是此类查询的一般概念。你可以弄清楚细节
select sum(case when some condition is met then 1 else 0 end) conditionsmet
, sum(case when that condition isnot met then 1 else 0 end) conditionsnotmet