简体   繁体   English

需要在MS Access(2010)中创建一个SQL语句来计算一个范围内的多个记录

[英]Need to create a SQL statement in MS Access (2010) to count multiple records in a range

I have two tables in MS access. 我在MS Access中有两个表。 Need to create a table that lookups the ranges in table1 and counts the records in table2, within that range. 需要创建一个表来查找table1中的范围并计算该范围内table2中的记录。

Table1 表格1

FROM        TO
00100000    00799999
00800000    00899999
00900000    01599999
01600000    01899999

Table2 表2

Acct
00103614
00103615
00103624
00103626
00104001
00104002
00104003
00104004
00104302
00104400
00104401
00104404
00104406
00104407
01622345
01622347
01622353
01622357
01622359
01622362
01622365
01622366
01622368

Desired output: 所需的输出:

FROM        TO          Count
00100000    00799999    50
00800000    00899999    10
00900000    01599999     0
01600000    01899999    42

Thanks Frank 谢谢弗兰克

I'll do something like this: 我会做这样的事情:

select count(*) as count, table1.tfrom, table1.to
from table2, table1
where table2.acct between table1.tfrom and table1.to
group by table1.tfrom, table1.to;

please avoid "SQL" words like from and to in table/column names 请避免在表/列名称中使用诸如“ from and to”之类的“ SQL”单词

Use correlated subquery : 使用相关子查询:

select *, (select count(*) 
           from table2 t2 
           where t2.acct >= t1.from and 
                 t2.acct <= t1.to
          ) as Count
from table1 t1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM