SQL 微软访问
我在 Microsoft Access 中有一个事务表,其中包含许多供应商的许多事务。我需要确定每个供应商是否有连续的交易编号。我不知道顺序是什么,也不知道每个供应商的交易数量。我需要编写一个 SQL 来标识供应商的顺序编号并将字段设置为“1”(如果存在)。我正在考虑运行嵌套循环,首先确定每个供应商的交易数量,然后循环比较这些交易的交易数量。有人可以帮我解决这个问题吗?
I have a table of transactions in Microsoft Access that contains many transactions for many vendors. I need to identify if there is sequential transaction numbering for each vendor. I don't know what the sequence will be or the number of transactions per vendor. I need to write a SQL that identifies sequential numbering for vendors and sets a field to '1' if present. I was thinking of running nested loops that first determine number of transactions per vendor then loops through those transactions comparing the transaction numbers. Can anybody help me with this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要查找一个连续的集合(2 个记录,其中一个交易编号跟随另一个交易编号):
To find one sequential set (2 records where one transaction number follows the other):
我不确定这是最直接的方法,但我认为它可行。对使用多个步骤表示歉意,但 Jet 4.0 强制要求这样做。**
我假设所有
transactionId
值都是正整数,并且序列是一组均匀间隔的transactionId< /code> 按
vendorId
值。我进一步假设(vendorId, transactionId)
上有一个键。第一步,消除无效行,例如需要至少三行才能确定序列(所有其他行是否通过或失败?);可能也想过滤掉这里的其他垃圾(例如具有
NULL
值的行/组):找到每个供应商的最低值(稍后会派上用场):
使所有序列从零开始(
transactionId_base_zero
),通过减去每个供应商的最小值:根据
MAX
、MIN
和预测步长值(相邻序列值之间的差异) COUNT
为每个供应商设置值:测试每个序列值的预测步值是否成立,即(伪代码)
this_transactionId - step_value =prior_transactionId
(省略最低的transactionId
) code> 因为它没有先前的值!):上述查询应返回
transactionId
值不连续的供应商的vendorId
。** 在我的辩护中,我遇到了 Jet 4.0 的几个错误,我必须编写解决方法。是的,我确实知道 Jet 4.0(或其 OLE DB 提供程序)中存在错误,因为 a) 我使用 SQL Server 仔细检查了结果,b) 它们不符合逻辑! (甚至 SQL 自己奇怪的 3VL 逻辑:)
I'm not sure this is the most straightforward approach but I think it could work. Apologies for using multiple steps but Jet 4.0 kind of forces one to do so.**
I've assumed all
transactionId
values are positive integers and that a sequence is a set of evenly spacedtransactionId
values byvendorId
. I further assume there is a key on(vendorId, transactionId)
.First step, elmininate invalid rows e.g. need at least three rows to be able to determine a sequence (do all other rows pass or fail?); may want to filter other junk out here too (e.g. rows/groups with
NULL
values):Find the lowest value for each vendor (comes in handy later):
Make all sequences start at zero (
transactionId_base_zero
) by subtracting the lowest value for each vendor:Predict the step value (difference between adjacent sequence values) based on the
MAX
,MIN
andCOUNT
set values for each vendor:Test that the predicted step value hold true for each squence value i.e. (pseudo code)
this_transactionId - step_value = prior_transactionId
(omit the lowesttransactionId
because it doesn't have a prior value!):The above query should return the
vendorId
of vendors whosetransactionId
values are not sequential.** In my defense, I ran into a couple of bugs Jet 4.0 I had to code around workaround. Yes, I do know the bugs are in Jet 4.0 (or its OLE DB provider) because a) I double checked results using SQL Server and b) they defy logic! (even SQL's own strange 3VL logic :)
我将使用一个查询来查找任何供应商的编号间隙,如果返回任何记录,则说明您没有对所有供应商进行连续编号。
其作用是,对于表中的每条记录,在同一个表中查找编号最小的其他 transactionID,该 transactionID 对应于同一供应商,并且具有比第一个编号更高的 transactionID。如果该记录的 transactionID 值比第一个记录中的值高出一个以上,则表示供应商的编号存在差距。
编辑:根据要求更改了上面的变量名称。
I would use a query that finds gaps in numbering for any vendor, and if that returns any records, then you do not have sequential numbering for all vendors.
What this does is, for each record in the table, look for the lowest-numbered other transactionID in the same table that is for the same vendor and has a higher-numbered transactionID than the first one. If that the transactionID value of that record is more than one higher than the value in the first record, that represents a gap in numbering for the vendor.
Edit: Changed variable names above as requested.