繁体   English   中英

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

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

我在MS Access中有两个表。 需要创建一个表来查找table1中的范围并计算该范围内table2中的记录。

表格1

FROM        TO
00100000    00799999
00800000    00899999
00900000    01599999
01600000    01899999

表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

所需的输出:

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

谢谢弗兰克

我会做这样的事情:

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;

请避免在表/列名称中使用诸如“ from and to”之类的“ SQL”单词

使用相关子查询:

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