Access SQL如何在SELECT查询中进行增量

发布于 2024-11-15 05:01:01 字数 168 浏览 3 评论 0原文

我有一个 SQL 查询给我 X 个结果,我希望查询输出有一个名为 计数使查询像这样:

count id section
1     15    7
2     3     2
3     54    1
4     7     4

我怎样才能做到这一点?

I Have an SQL query giving me X results, I want the query output to have a coulmn called
count making the query somthing like this:

count id section
1     15    7
2     3     2
3     54    1
4     7     4

How can I make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

紫瑟鸿黎 2024-11-22 05:01:01

我见过的唯一技巧是,如果您有一个连续的 id 字段,您可以创建一个新字段,其中每个记录的值为 1。然后对该字段进行运行求和。

添加到您的查询

DSum("[New field with 1 in it]","[Table Name]","[ID field]<=" & [ID Field]) 
as counterthing

这应该会在 Access 中产生一个顺序计数,这就是我认为您想要的。

HTH。

(从 Rob Mills 那里盗来的:
http://www.access-programmers.co.uk/论坛/showthread.php?p=160386)

The only trick I have seen is if you have a sequential id field, you can create a new field in which the value for each record is 1. Then you do a running sum of that field.

Add to your query

DSum("[New field with 1 in it]","[Table Name]","[ID field]<=" & [ID Field]) 
as counterthing

That should produce a sequential count in Access which is what I think you want.

HTH.

(Stolen from Rob Mills here:
http://www.access-programmers.co.uk/forums/showthread.php?p=160386)

三生一梦 2024-11-22 05:01:01

那么在你的例子中,“count”是派生的序列号?我不知道使用什么模式来确定 id=15 的计数必须为 1,id=3 的计数必须为 2。

count id section
1     15    7
2     3     2
3     54    1
4     7     4

如果 id 包含唯一值,并且您按 id 排序,您可能会得到以下结果:

count id section
1     3     2
2     7     4
3     15    7
4     54    1

在我看来,mikeY 的 DSum 方法可以工作。或者,您可以使用不同的排名查询方法,如 Allen Browne 在此页面

编辑< /strong>:您可以使用 DCount 而不是 DSum。我不知道两者之间的速度比较如何,但 DCount 避免在表中创建一个字段,只是为了为每行存储 1。

DCount("*","YourTableName","id<=" & [id]) AS counter

无论您使用 DCount 还是 DSum,如果 id 值不唯一,计数器值都可能包含重复项。如果 id 是主键,不用担心。

So in your example, "count" is the derived sequence number? I don't see what pattern is used to determine the count must be 1 for id=15 and 2 for id=3.

count id section
1     15    7
2     3     2
3     54    1
4     7     4

If id contained unique values, and you order by id you could have this:

count id section
1     3     2
2     7     4
3     15    7
4     54    1

Looks to me like mikeY's DSum approach could work. Or you could use a different approach to a ranking query as Allen Browne described at this page

Edit: You could use DCount instead of DSum. I don't know how the speed would compare between the two, but DCount avoids creating a field in the table simply to store a 1 for each row.

DCount("*","YourTableName","id<=" & [id]) AS counter

Whether you go with DCount or DSum, the counter values can include duplicates if the id values are not unique. If id is a primary key, no worries.

仙女山的月亮 2024-11-22 05:01:01

坦率地说,我不明白您想要什么,但如果您想要的只是在表单上显示序列号,则可以使用绑定到表单的 CurrentRecord 属性的控件。具有 ControlSource =CurrentRecord 的控件将具有一个始终准确的按顺序排列的“记录号”,并且当窗体的 Recordsource 更改时(这可能是也可能不是需要的),该记录号将会更新。

然后,如果您愿意,可以使用该数字在表单中导航。

但这可能与您正在寻找的不一样——我根本无法从您发布的问题和评论中的“澄清”中看出。

I frankly don't understand what it is you want, but if all you want is a sequence number displayed on your form, you can use a control bound to the form's CurrentRecord property. A control with the ControlSource =CurrentRecord will have an always-accurate "record number" that is in sequence, and that will update when the form's Recordsource changes (which may or may not be desirable).

You can then use that number to navigate around the form, if you like.

But this may not be anything like what you're looking for -- I simply can't tell from the question you've posted and the "clarifications" in comments.

冷︶言冷语的世界 2024-11-22 05:01:01

好吧,我想这足以构成一个答案:以下链接指定了两种方法: http://www.techrepublic.com/blog/microsoft-office/an-access-query-that-returns-every-nth-record/

第一种方法假设您有ID 值并使用 DCount (类似于 @mikeY 的解决方案)。

第二种方法假设您可以创建一个 VBA 函数,该函数将为记录集中的每个记录运行一次,并且每次您想要运行计数时都需要手动重置(使用一些 VBA) - 因为它使用“静态” 的价值来反其道而行之。

只要您有合理的数字(数百,而不是数千)或记录,第二种方法对我来说似乎是最简单/最强大的。

Alright, I guess this comes close enough to constitute an answer: the following link specifies two approaches: http://www.techrepublic.com/blog/microsoft-office/an-access-query-that-returns-every-nth-record/

The first approach assumes that you have an ID value and uses DCount (similar to @mikeY's solution).

The second approach assumes you're OK creating a VBA function that will run once for EACH record in the recordset, and will need to be manually reset (with some VBA) every time you want to run the count - because it uses a "static" value to run its counter.

As long as you have reasonable numbers (hundreds, not thousands) or records, the second approach looks like the easiest/most powerful to me.

凉栀 2024-11-22 05:01:01

如果模块可用,则可以从每个记录调用此函数。

示例:incrementingCounterTimeFlaged(10,[anyField]) 应该为您的查询行提供一个从 0 开始递增的 int。

'provides incrementing int values 0 to n
'resets to 0 some seconds after first call
Function incrementingCounterTimeFlaged(resetAfterSeconds As Integer,anyfield as variant) As Integer

Static resetAt As Date
Static i As Integer
'if reset date < now() set the flag and return 0

If DateDiff("s", resetAt, Now()) > 0 Then
    resetAt = DateAdd("s", resetAfterSeconds, Now())
    i = 0
    incrementingCounterTimeFlaged = i
'if reset date > now increments and returns
Else
    i = i + 1
    incrementingCounterTimeFlaged = i
End If    
End Function

This function can be called from each record if available from a module.

Example: incrementingCounterTimeFlaged(10,[anyField]) should provide your query rows an int incrementing from 0.

'provides incrementing int values 0 to n
'resets to 0 some seconds after first call
Function incrementingCounterTimeFlaged(resetAfterSeconds As Integer,anyfield as variant) As Integer

Static resetAt As Date
Static i As Integer
'if reset date < now() set the flag and return 0

If DateDiff("s", resetAt, Now()) > 0 Then
    resetAt = DateAdd("s", resetAfterSeconds, Now())
    i = 0
    incrementingCounterTimeFlaged = i
'if reset date > now increments and returns
Else
    i = i + 1
    incrementingCounterTimeFlaged = i
End If    
End Function
人海汹涌 2024-11-22 05:01:01

SQL中的自动增量

SELECT (Select COUNT(*) FROM table A where A.id<=b.id),B.id,B.Section FROM table AS B ORDER BY B.ID Asc

autoincrement in SQL

SELECT (Select COUNT(*) FROM table A where A.id<=b.id),B.id,B.Section FROM table AS B ORDER BY B.ID Asc
¢蛋碎的人ぎ生 2024-11-22 05:01:01

您可以使用 SQL Server 2008 中的 ROW_NUMBER()

SELECT ROW_NUMBER() OVER (ORDER By ID DESC) RowNum,
    ID,
    Section
FROM myTable

然后 RowNum 显示行号序列。

You can use ROW_NUMBER() which is in SQL Server 2008

SELECT ROW_NUMBER() OVER (ORDER By ID DESC) RowNum,
    ID,
    Section
FROM myTable

Then RowNum displays sequence of row numbers.

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