Access SQL如何在SELECT查询中进行增量
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我见过的唯一技巧是,如果您有一个连续的 id 字段,您可以创建一个新字段,其中每个记录的值为 1。然后对该字段进行运行求和。
添加到您的查询
这应该会在 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
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)
那么在你的例子中,“count”是派生的序列号?我不知道使用什么模式来确定 id=15 的计数必须为 1,id=3 的计数必须为 2。
如果 id 包含唯一值,并且您按 id 排序,您可能会得到以下结果:
在我看来,mikeY 的 DSum 方法可以工作。或者,您可以使用不同的排名查询方法,如 Allen Browne 在此页面
编辑< /strong>:您可以使用 DCount 而不是 DSum。我不知道两者之间的速度比较如何,但 DCount 避免在表中创建一个字段,只是为了为每行存储 1。
无论您使用 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.
If id contained unique values, and you order by id you could have this:
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.
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.
坦率地说,我不明白您想要什么,但如果您想要的只是在表单上显示序列号,则可以使用绑定到表单的 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.
好吧,我想这足以构成一个答案:以下链接指定了两种方法: 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.
如果模块可用,则可以从每个记录调用此函数。
示例:incrementingCounterTimeFlaged(10,[anyField]) 应该为您的查询行提供一个从 0 开始递增的 int。
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.
SQL中的自动增量
autoincrement in SQL
您可以使用 SQL Server 2008 中的 ROW_NUMBER()
然后 RowNum 显示行号序列。
You can use ROW_NUMBER() which is in SQL Server 2008
Then RowNum displays sequence of row numbers.