SQL 微软访问

发布于 2024-11-02 03:19:49 字数 193 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单调的奢华 2024-11-09 03:19:49

要查找一个连续的集合(2 个记录,其中一个交易编号跟随另一个交易编号):

SELECT transactionId FROM tbl WHERE EXISTS 
  (SELECT * FROM tbl as t WHERE tbl.vendorId = t.vendorId 
   AND tbl.transactionId+1 = t.transactionId)

To find one sequential set (2 records where one transaction number follows the other):

SELECT transactionId FROM tbl WHERE EXISTS 
  (SELECT * FROM tbl as t WHERE tbl.vendorId = t.vendorId 
   AND tbl.transactionId+1 = t.transactionId)
三人与歌 2024-11-09 03:19:49

我不确定这是最直接的方法,但我认为它可行。对使用多个步骤表示歉意,但 Jet 4.0 强制要求这样做。**

我假设所有 transactionId 值都是正整数,并且序列是一组均匀间隔的 transactionId< /code> 按 vendorId 值。我进一步假设 (vendorId, transactionId) 上有一个键。

第一步,消除无效行,例如需要至少三行才能确定序列(所有其他行是否通过或失败?);可能也想过滤掉这里的其他垃圾(例如具有 NULL 值的行/组):

CREATE VIEW tbl1
AS
SELECT T1.vendorId, T1.transactionId
  FROM tbl AS T1
 WHERE EXISTS (
               SELECT T2.vendorId
                 FROM tbl AS T2
                WHERE T2.vendorId = T1.vendorId
                GROUP 
                   BY T2.vendorId
                HAVING COUNT(*) > 2
              );

找到每个供应商的最低值(稍后会派上用场):

CREATE VIEW tbl2
AS
SELECT vendorId, MIN(transactionId) AS transactionId_min
  FROM tbl1
 GROUP 
    BY vendorId;

使所有序列从零开始(transactionId_base_zero),通过减去每个供应商的最小值:

CREATE VIEW tbl3
AS
SELECT T1.vendorId, T1.transactionId, 
       T1.transactionId - T2.transactionId_min AS transactionId_base_zero
  FROM tbl1 AS T1
       INNER JOIN tbl2 AS T2
          ON T1.vendorId = T2.vendorId;

根据 MAXMIN预测步长值(相邻序列值之间的差异) COUNT 为每个供应商设置值:

CREATE VIEW tbl4
AS
SELECT vendorId, 
       MAX(transactionId_base_zero) / (COUNT(*) - 1)
          AS transactionId_predicted_step
  FROM tbl3;

测试每个序列值的预测步值是否成立,即(伪代码)this_transactionId - step_value =prior_transactionId(省略最低的transactionId) code> 因为它没有先前的值!):

SELECT DISTINCT T.vendorId
  FROM tbl3 AS T
 WHERE T.transactionId_base_zero > 0
       AND NOT EXISTS (
                       SELECT *
                         FROM tbl3 AS T3
                              INNER JOIN tbl4 AS T4
                                 ON T3.vendorId = T4.vendorId      
                        WHERE T.vendorId = T3.vendorId
                              AND T.transactionId_base_zero 
                                     - T4.transactionId_predicted_step 
                                        = T3.transactionId_base_zero
                      );

上述查询应返回 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 spaced transactionId values by vendorId. 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):

CREATE VIEW tbl1
AS
SELECT T1.vendorId, T1.transactionId
  FROM tbl AS T1
 WHERE EXISTS (
               SELECT T2.vendorId
                 FROM tbl AS T2
                WHERE T2.vendorId = T1.vendorId
                GROUP 
                   BY T2.vendorId
                HAVING COUNT(*) > 2
              );

Find the lowest value for each vendor (comes in handy later):

CREATE VIEW tbl2
AS
SELECT vendorId, MIN(transactionId) AS transactionId_min
  FROM tbl1
 GROUP 
    BY vendorId;

Make all sequences start at zero (transactionId_base_zero) by subtracting the lowest value for each vendor:

CREATE VIEW tbl3
AS
SELECT T1.vendorId, T1.transactionId, 
       T1.transactionId - T2.transactionId_min AS transactionId_base_zero
  FROM tbl1 AS T1
       INNER JOIN tbl2 AS T2
          ON T1.vendorId = T2.vendorId;

Predict the step value (difference between adjacent sequence values) based on the MAX, MIN and COUNT set values for each vendor:

CREATE VIEW tbl4
AS
SELECT vendorId, 
       MAX(transactionId_base_zero) / (COUNT(*) - 1)
          AS transactionId_predicted_step
  FROM tbl3;

Test that the predicted step value hold true for each squence value i.e. (pseudo code) this_transactionId - step_value = prior_transactionId (omit the lowest transactionId because it doesn't have a prior value!):

SELECT DISTINCT T.vendorId
  FROM tbl3 AS T
 WHERE T.transactionId_base_zero > 0
       AND NOT EXISTS (
                       SELECT *
                         FROM tbl3 AS T3
                              INNER JOIN tbl4 AS T4
                                 ON T3.vendorId = T4.vendorId      
                        WHERE T.vendorId = T3.vendorId
                              AND T.transactionId_base_zero 
                                     - T4.transactionId_predicted_step 
                                        = T3.transactionId_base_zero
                      );

The above query should return the vendorId of vendors whose transactionId 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 :)

め七分饶幸 2024-11-09 03:19:49

我将使用一个查询来查找任何供应商的编号间隙,如果返回任何记录,则说明您没有对所有供应商进行连续编号。

SELECT *
FROM tblTransaction As T1
WHERE (
    SELECT TOP 1 T2.transactionID
    FROM tblTransaction As T2
    WHERE T1.vendorID = T2.vendorID AND
          T1.transactionID < T2.transactionID
    ORDER BY T2.transactionID
) - T1.transactionID > 1

其作用是,对于表中的每条记录,在同一个表中查找编号最小的其他 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.

SELECT *
FROM tblTransaction As T1
WHERE (
    SELECT TOP 1 T2.transactionID
    FROM tblTransaction As T2
    WHERE T1.vendorID = T2.vendorID AND
          T1.transactionID < T2.transactionID
    ORDER BY T2.transactionID
) - T1.transactionID > 1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文