将字段连接到值范围

时间:2019-06-19 17:12:19

标签: sql ms-access

我正在尝试从引用另一个表中的事务的表中提取数据。我不知道如何获得所需的输出。

表1:

+--------+-------------+---------+
| Widget | SequenceNbr | Amount  |
+--------+-------------+---------+
| Thing1 |       12345 |  100.00 |
| Thing1 |       12346 |  110.00 |
| Thing2 |       12345 |   98.25 |
| Thing2 |       12347 |  877.99 |
| Thing3 |       12333 |  638.00 |
+--------+-------------+---------+

表2:

+----------+------------------+----------------+
| BatchNbr | BeginSequenceNbr | EndSequenceNBr |
+----------+------------------+----------------+
|     9998 |            12300 |          12345 |
|     9999 |            12346 |          12350 |
+----------+------------------+----------------+

我尝试为group by / first和不同的联接方案使用last进行创建。

样本查询:

SELECT 
    Table1.[Widget],
    Table1.[SequenceNbr],
    Table1.[Amount], 
    Table2.[BatchNbr]
FROM 
    Table2 INNER JOIN Table1 ON 
    (Table2.BeginSequenceNbr) AND 
    (Table2.EndSequenceNbr = Table1.[SequenceNbr])
WHERE 
    (((Table1.[SequenceNbr]) Between [Enter Begin Sequence Nbr] And [Enter End Sequence Nbr])));

如果参数查询序列范围1230012346,则所需的输出为

Thing1, 12345, 100.00, 9998
Thing1, 12346, 110.00, 9999
Thing2, 12345, 98.25, 9998
Thing3, 12333, 638.00, 9998

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

select
    t1.widget, 
    t1.sequencenbr, 
    t1.amount, 
    t2.batchnbr
from
    table1 t1 inner join table2 t2 on
    t1.sequencenbr >= t2.beginsequencenbr and  
    t1.sequencenbr <= t2.endsequencenbr

请注意,MS Access将无法代表查询设计器中的联接,但是MS Access使用的JET数据库引擎仍应能够执行此有效的SQL。