[英]MS Access SQL (2010) - How to create a calculated column based multiple records “quickly”?
[英]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.