简体   繁体   English

SQL 访问数据库,它允许我访问 select 'n' 连续数字

[英]SQL to Access database which allows me to select 'n' consecutive numbers

This maybe a bit of a big favor to ask, but need a little help trying to get certain set of data from an access database using VB.NET.这可能是一个很大的问题,但需要一点帮助来尝试使用 VB.NET 从访问数据库中获取某些数据集。 I have found a great article and the question is what I want basically, however the solutions given are unfortunately not for access.我找到了一篇很棒的文章,问题基本上是我想要的,但是不幸的是,给出的解决方案不适用于访问。

See the thread here: https://dba.stackexchange.com/questions/36943/find-n-consecutive-free-numbers-from-table请参阅此处的线程: https://dba.stackexchange.com/questions/36943/find-n-consecutive-free-numbers-from-table

The code that works for me and would be perfect is here: http://sqlfiddle.com/#!1/a2633/2适合我并且完美的代码在这里: http://sqlfiddle.com/#!1/a2633/2

Ideally I'd have a proper SQL database but unfortunately I'm stuck with MS Access, and I know it doesn't accept ROW_NUMBER or PARTITION BY ... how do I know?理想情况下,我有一个合适的 SQL 数据库,但不幸的是我被 MS Access 卡住了,我知道它不接受ROW_NUMBERPARTITION BY ...我怎么知道? I've tried and googled:~)我已经尝试并用谷歌搜索了:~)

Basically I have a list of numbers in a database as below:基本上我在数据库中有一个数字列表,如下所示:

访问表 My current SQL is我现在的 SQL 是

SELECT TOP 5 
    [ID], [UIDNo] 
FROM 
    [tblUIDS] 
WHERE 
    [Requester] IS NULL 
ORDER BY 
    ID

The 5 is a variable where the user says i want x amount, so the SQL statement returns the top number of UIDNos, which is grand, it then does an INSERT statement to the requester. 5 是一个变量,用户说我想要 x 数量,因此 SQL 语句返回 UIDNos 的最高数量,这很重要,然后它向请求者INSERT语句。 However id like it to be able to return 5 UIDNo that are sequential.然而,我喜欢它能够返回 5 个连续的 UIDNo。

So based on the above image if the user wants 2 UIDNo it will return 00004 & 00005, instead of just returning 00001 & 00004. If the user wants 6 UIDNo it will return 00032-00037 etc etc.因此,基于上图,如果用户想要 2 个 UIDNo,它将返回 00004 和 00005,而不仅仅是返回 00001 和 00004。如果用户想要 6 个 UIDNo,它将返回 00032-00037 等等。

Does using VB.NET even allow me to query a MS Access database like this?使用 VB.NET 甚至允许我像这样查询 MS Access 数据库吗? I'm open to suggestions as well if its going to be easier another way...如果以另一种方式更容易,我也愿意接受建议...

Thanks in advance提前致谢

No temp table required.不需要临时表。 Yes, code is pretty complex, but you can follow it.是的,代码非常复杂,但您可以按照它进行操作。

Many thanks to the references you cited.非常感谢您引用的参考文献。

Currently set to get 5 records目前设置为获得 5 条记录

SELECT TOP 5 [id],
             [uidno]
FROM   consec
WHERE  uidno >= (SELECT First(first_number) AS StartAt
                 FROM   (SELECT Min(uidno) AS first_number,
                                Count(*)   AS ct_free
                         FROM   (SELECT id,
                                        uidno,
                                        Dcount("uidno", "consec",
                                        "uidno <=" & [uidno])
                                        AS
                                        Counter,
                                        uidno - counter
                                        AS Grp
                                 FROM   (SELECT TOP 90000 [id],
                                                          [uidno]
                                         FROM   consec
                                         WHERE  [requester] IS NULL
                                         ORDER  BY uidno,
                                                   id) AS in1) x
                         GROUP  BY grp
                         HAVING Count(*) >= 5
                         ORDER  BY grp)) 

Delivers提供

id      uidno
615092  32
615093  33
615094  34
615095  35
615096  36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM